-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
[Feature]: json_schema in response support for Anthropic #6748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c3a2c77
_convert_tool_response_to_message
ishaan-jaff a866bdb
fix ModelResponseIterator
ishaan-jaff eaf5723
fix test_json_response_format
ishaan-jaff f5f36fb
test_json_response_format_stream
ishaan-jaff ce235fa
fix _convert_tool_response_to_message
ishaan-jaff 1cdee5a
use helper _handle_json_mode_chunk
ishaan-jaff d77fd30
fix _process_response
ishaan-jaff 26f7a6b
unit testing for test_convert_tool_response_to_message_no_arguments
ishaan-jaff 0f75cd1
update doc for JSON mode
ishaan-jaff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,8 +33,10 @@ | |
| ) | ||
| from litellm.adapters.anthropic_adapter import anthropic_adapter | ||
| from litellm.types.llms.anthropic import AnthropicResponse | ||
|
|
||
| from litellm.types.utils import GenericStreamingChunk, ChatCompletionToolCallChunk | ||
| from litellm.types.llms.openai import ChatCompletionToolCallFunctionChunk | ||
| from litellm.llms.anthropic.common_utils import process_anthropic_headers | ||
| from litellm.llms.anthropic.chat.handler import AnthropicChatCompletion | ||
| from httpx import Headers | ||
| from base_llm_unit_tests import BaseLLMChatTest | ||
|
|
||
|
|
@@ -694,3 +696,91 @@ def test_pdf_handling(self, pdf_messages): | |
| assert _document_validation["type"] == "document" | ||
| assert _document_validation["source"]["media_type"] == "application/pdf" | ||
| assert _document_validation["source"]["type"] == "base64" | ||
|
|
||
|
|
||
| def test_convert_tool_response_to_message_with_values(): | ||
| """Test converting a tool response with 'values' key to a message""" | ||
| tool_calls = [ | ||
| ChatCompletionToolCallChunk( | ||
| id="test_id", | ||
| type="function", | ||
| function=ChatCompletionToolCallFunctionChunk( | ||
| name="json_tool_call", | ||
| arguments='{"values": {"name": "John", "age": 30}}', | ||
| ), | ||
| index=0, | ||
| ) | ||
| ] | ||
|
|
||
| message = AnthropicChatCompletion._convert_tool_response_to_message( | ||
| tool_calls=tool_calls | ||
| ) | ||
|
|
||
| assert message is not None | ||
| assert message.content == '{"name": "John", "age": 30}' | ||
|
|
||
|
|
||
| def test_convert_tool_response_to_message_without_values(): | ||
| """ | ||
| Test converting a tool response without 'values' key to a message | ||
|
|
||
| Anthropic API returns the JSON schema in the tool call, OpenAI Spec expects it in the message. This test ensures that the tool call is converted to a message correctly. | ||
|
|
||
| Relevant issue: https://github.com/BerriAI/litellm/issues/6741 | ||
| """ | ||
| tool_calls = [ | ||
| ChatCompletionToolCallChunk( | ||
| id="test_id", | ||
| type="function", | ||
| function=ChatCompletionToolCallFunctionChunk( | ||
| name="json_tool_call", arguments='{"name": "John", "age": 30}' | ||
| ), | ||
| index=0, | ||
| ) | ||
| ] | ||
|
|
||
| message = AnthropicChatCompletion._convert_tool_response_to_message( | ||
| tool_calls=tool_calls | ||
| ) | ||
|
|
||
| assert message is not None | ||
| assert message.content == '{"name": "John", "age": 30}' | ||
|
|
||
|
|
||
| def test_convert_tool_response_to_message_invalid_json(): | ||
| """Test converting a tool response with invalid JSON""" | ||
| tool_calls = [ | ||
| ChatCompletionToolCallChunk( | ||
| id="test_id", | ||
| type="function", | ||
| function=ChatCompletionToolCallFunctionChunk( | ||
| name="json_tool_call", arguments="invalid json" | ||
| ), | ||
| index=0, | ||
| ) | ||
| ] | ||
|
|
||
| message = AnthropicChatCompletion._convert_tool_response_to_message( | ||
| tool_calls=tool_calls | ||
| ) | ||
|
|
||
| assert message is not None | ||
| assert message.content == "invalid json" | ||
|
|
||
|
|
||
| def test_convert_tool_response_to_message_no_arguments(): | ||
| """Test converting a tool response with no arguments""" | ||
| tool_calls = [ | ||
| ChatCompletionToolCallChunk( | ||
| id="test_id", | ||
| type="function", | ||
| function=ChatCompletionToolCallFunctionChunk(name="json_tool_call"), | ||
| index=0, | ||
| ) | ||
| ] | ||
|
|
||
| message = AnthropicChatCompletion._convert_tool_response_to_message( | ||
| tool_calls=tool_calls | ||
| ) | ||
|
|
||
| assert message is None | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ishaan-jaff this code should not be in the handler function as it's about llm translation.
it should be in the transformation.py or prompt_factory file
acknowledging prior code was in handler - it seems like that was missed during a refactor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey I think a lot of the previous code for this was written by you @krrishdholakia
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the PR I looked at - had no idea why this logic was in the handler to begin with but assumed there must have been a reason you had f2401d6#diff-4b817e67059fdef6b8fabcd1e356d6597ffdde6ff251a1233a01d41b8805207b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed here @krrishdholakia #6834