-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: support tool_choice with specific tool names in LiteLLM streaming (fixes #1846) #1929
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
Changes from 6 commits
2f85765
8abed69
fca3ed5
1d20846
ae1ff2d
d9aa6da
3c5690a
a0ba8a2
150db69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| ) | ||
| from openai.types.chat.chat_completion_message_function_tool_call import Function | ||
| from openai.types.responses import Response | ||
| from openai.types.responses.tool_choice_function import ToolChoiceFunction | ||
|
|
||
| from ... import _debug | ||
| from ...agent_output import AgentOutputSchemaBase | ||
|
|
@@ -367,15 +368,48 @@ async def _fetch_response( | |
| if isinstance(ret, litellm.types.utils.ModelResponse): | ||
| return ret | ||
|
|
||
| # Convert tool_choice to the correct type for Response | ||
| # tool_choice can be a Literal, ToolChoiceFunction, | ||
| # dict from ChatCompletions Converter, or omit | ||
| response_tool_choice: Literal["auto", "required", "none"] | ToolChoiceFunction | ||
| if tool_choice is omit: | ||
| response_tool_choice = "auto" | ||
| elif isinstance(tool_choice, ToolChoiceFunction): | ||
| # Already a ToolChoiceFunction, use directly | ||
| response_tool_choice = tool_choice | ||
| elif isinstance(tool_choice, dict): | ||
| # Convert from ChatCompletions format dict to ToolChoiceFunction | ||
| # ChatCompletions Converter returns: {"type": "function", "function": {"name": "..."}} | ||
| func_data = tool_choice.get("function") | ||
| if ( | ||
| tool_choice.get("type") == "function" | ||
| and func_data is not None | ||
| and isinstance(func_data, dict) | ||
| ): | ||
| tool_name = func_data.get("name") | ||
| if isinstance(tool_name, str) and tool_name: # Ensure non-empty string | ||
| response_tool_choice = ToolChoiceFunction(type="function", name=tool_name) | ||
| else: | ||
| # Fallback to auto if name is missing or invalid | ||
| response_tool_choice = "auto" | ||
| else: | ||
| # Fallback to auto if unexpected format | ||
| response_tool_choice = "auto" | ||
| elif tool_choice in ("auto", "required", "none"): | ||
| from typing import cast | ||
|
|
||
| response_tool_choice = cast(Literal["auto", "required", "none"], tool_choice) | ||
| else: | ||
| # Fallback to auto for any other case | ||
| response_tool_choice = "auto" | ||
|
|
||
| response = Response( | ||
| id=FAKE_RESPONSES_ID, | ||
| created_at=time.time(), | ||
| model=self.model, | ||
| object="response", | ||
| output=[], | ||
| tool_choice=cast(Literal["auto", "required", "none"], tool_choice) | ||
| if tool_choice is not omit | ||
| else "auto", | ||
| tool_choice=response_tool_choice, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this, and it’s still not fixed, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks your test, I will test it again later! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Fixed in commit fca3ed5 and verified with integration testing. Root cause: The initial fix incorrectly assumed LiteLLM uses The fix: Now correctly handles the nested dict structure Verification: Integration test confirms that when Test output: The fix is now working correctly! |
||
| top_p=model_settings.top_p, | ||
| temperature=model_settings.temperature, | ||
| tools=[], | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.