Skip to content

Conversation

@KeremTurgutlu
Copy link
Contributor

@KeremTurgutlu KeremTurgutlu commented Dec 9, 2025

Fixes #64

This PR upgrades litellm>1.80.5 and fixes issues related to web search tool being added to Message.tool_calls

TL;DR: Keeping the server tool call in tool_calls causes the following issue:

`tool_use` ids were found without `tool_result` blocks immediately after: srvtoolu_01AQnDc29pQaynmttMCch4TZ. Each `tool_use` block must have a corresponding `tool_result` block in the next message.

So I've ended up removing web search from the tool_calls which is the current behavior before this upgrade. This not ideal as model has no idea whether it has used web search or not. This can be seen in this issue which from the current main branch and not related to this upgrade.

Iterative Fixes Tried

  • Open Litellm Issue mentioning additional {} in function arguments when stream=True. This causes a JSON decoding error when tool_calls is provided in the next turn.

This can be fixed by _strip_fargs().

  • But a bigger issue is that web_search is now included in tool_calls which are the tools expected to be run. So we can fix this by filtering out server tools during tool response generation which will skip server tools:
if tcs := _filter_srvtools(m.tool_calls):
  • The other problem is related to parallel use of web search and custom tools. When this happens litellm response doesn't include the web search results, which is a server side tool. In the next turn this raises a missing tool response error. This happens regardless of stream=True/False, for example:
q = "Search the web for the avg weight, in kgs, of male African and Asian elephants. And also add 123123 and 141234 using the tool."

model = "claude-sonnet-4-5"
messages = [{"role": "user", "content": q}]
r = completion(model=model, messages=messages, tools=[lite_mk_func(add_numbers)],web_search_options={"search_context_size": "low"})
dict(r)

>>>
{'id': 'chatcmpl-e7572d0c-3025-4d77-acba-7b6ec6957d1d',
 'created': 1765284440,
 'model': 'claude-sonnet-4-5-20250929',
 'object': 'chat.completion',
 'system_fingerprint': None,
 'choices': [Choices(finish_reason='tool_calls', index=0, message=Message(content=None, role='assistant', tool_calls=[ChatCompletionMessageToolCall(index=0, function=Function(arguments='{"query": "average weight male African Asian elephants kg"}', name='web_search'), id='srvtoolu_01V39Cv4nbrfCENz9ToTR8Xt', type='function'), ChatCompletionMessageToolCall(index=1, function=Function(arguments='{"a": 123123, "b": 141234}', name='add_numbers'), id='toolu_01LEHYGHBpyWyeiyz76qE86g', type='function')], function_call=None, provider_specific_fields={'citations': None, 'thinking_blocks': None}))],
 'usage': Usage(completion_tokens=115, prompt_tokens=2345, total_tokens=2460, completion_tokens_details=None, prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=None, cached_tokens=0, text_tokens=None, image_tokens=None, cache_creation_tokens=0, cache_creation_token_details=CacheCreationTokenDetails(ephemeral_5m_input_tokens=0, ephemeral_1h_input_tokens=0)), cache_creation_input_tokens=0, cache_read_input_tokens=0)}

This is can be fixed by removing the server tool call from the history when tool_calls includes a mix:

m.tool_calls = _strip_srvtools(m.tool_calls)

A separate issue to fix this is also created.

Update: Even just keeping the server tool call in tool_calls causes an issue:

Note: Recreated cachy.jsonl from scratch to make sure that all the completions work as expected.

@KeremTurgutlu
Copy link
Contributor Author

KeremTurgutlu commented Dec 10, 2025

@jph00 I've created an issue for the anthropic web search problems in litellm BerriAI/litellm#17737 and it looks like they've already merged a fix. According to this comment it might not be working for streaming yet though.

Their fix make sures the web search results are now inlcuded during Anthropic messages construction, so we don't need to remove it from tool_calls, and AI will now know it has called web search. Only change we'd need is to exclude web_search tool when creating the tool responses.

I would say let's wait for them to completely fix it and make a new release with it, and then we can update this PR to use that newer release with the required fixes.

Note: I've tested previously failing example from the main branch and it works fine:

=== First API call (web search + tool use) ===
Response content: Based on the search results:

- Male African Savanna elephants average 5,000 kg
- Male Asian elephants weigh on average about 3,600 kg
Tool calls: ['web_search', 'add_numbers']

Executing add_numbers(5000, 3600) = 8600

=== Second API call (continuation) ===
SUCCESS! Response: Based on the search results:

- Male African Savanna elephants average 5,000 kg
- Male Asian elephants weigh on average about 3,600 kg

**Total: 8,600 kg**
=== Messages ===

==================================================
Test result: PASSED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upgrade litellm to >1.80.5

2 participants