Skip to content

Commit 48c8981

Browse files
authored
[Bug Fix] /messages endpoint - ensure tool use arguments are returned for non-anthropic models (#13638)
* bug fix _translate_streaming_openai_chunk_to_anthropic * test test_translate_streaming_openai_chunk_to_anthropic_with_partial_json
1 parent 17db9ed commit 48c8981

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def _translate_streaming_openai_chunk_to_anthropic(
489489
text: str = ""
490490
partial_json: Optional[str] = None
491491
for choice in choices:
492-
if choice.delta.content is not None:
492+
if choice.delta.content is not None and len(choice.delta.content) > 0:
493493
text += choice.delta.content
494494
elif choice.delta.tool_calls is not None:
495495
partial_json = ""
@@ -499,7 +499,6 @@ def _translate_streaming_openai_chunk_to_anthropic(
499499
and tool.function.arguments is not None
500500
):
501501
partial_json += tool.function.arguments
502-
503502
if partial_json is not None:
504503
return "input_json_delta", ContentJsonBlockDelta(
505504
type="input_json_delta", partial_json=partial_json

litellm/proxy/proxy_config.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
model_list:
2-
- model_name: vertex_ai/*
3-
litellm_params:
4-
model: vertex_ai/*
52
- model_name: bedrock/converse/us.anthropic.claude-sonnet-4-20250514-v1:0
63
litellm_params:
74
model: bedrock/converse/us.anthropic.claude-sonnet-4-20250514-v1:0

tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
from litellm.llms.anthropic.experimental_pass_through.adapters.transformation import (
1212
LiteLLMAnthropicMessagesAdapter,
1313
)
14-
from litellm.types.llms.anthropic import AnthropicMessagesUserMessageParam, AnthopicMessagesAssistantMessageParam
14+
from litellm.types.llms.anthropic import (
15+
AnthopicMessagesAssistantMessageParam,
16+
AnthropicMessagesUserMessageParam,
17+
)
1518
from litellm.types.llms.openai import ChatCompletionAssistantToolCall
1619
from litellm.types.utils import (
1720
ChatCompletionDeltaToolCall,
21+
Choices,
1822
Delta,
1923
Function,
20-
StreamingChoices,
21-
Choices,
2224
Message,
25+
StreamingChoices,
2326
)
2427

2528

@@ -148,3 +151,44 @@ def test_translate_openai_content_to_anthropic_empty_function_arguments():
148151
assert result[0].id == "call_empty_args"
149152
assert result[0].name == "test_function"
150153
assert result[0].input == {}, "Empty function arguments should result in empty dict"
154+
155+
156+
157+
def test_translate_streaming_openai_chunk_to_anthropic_with_partial_json():
158+
"""Test that partial tool arguments are correctly handled as input_json_delta."""
159+
choices = [
160+
StreamingChoices(
161+
finish_reason=None,
162+
index=1,
163+
delta=Delta(
164+
provider_specific_fields=None,
165+
content='',
166+
role='assistant',
167+
function_call=None,
168+
tool_calls=[
169+
ChatCompletionDeltaToolCall(
170+
id=None,
171+
function=Function(arguments=': "San ', name=None),
172+
type='function',
173+
index=0
174+
)
175+
],
176+
audio=None,
177+
),
178+
logprobs=None,
179+
)
180+
]
181+
182+
(
183+
type_of_content,
184+
content_block_delta,
185+
) = LiteLLMAnthropicMessagesAdapter()._translate_streaming_openai_chunk_to_anthropic(
186+
choices=choices
187+
)
188+
189+
print("Type of content:", type_of_content)
190+
print("Content block delta:", content_block_delta)
191+
192+
assert type_of_content == "input_json_delta"
193+
assert content_block_delta["type"] == "input_json_delta"
194+
assert content_block_delta["partial_json"] == ': "San '

0 commit comments

Comments
 (0)