Repository navigation
fix(advanced): don't force tool_choice on Opus/Sonnet 5.5, bump 0.19.10 [PC-5488] #1206
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
Open
tudormatei1
wants to merge
2
commits into
main
Choose a base branch
from
fix/advanced-unforced-tool-choice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| """Structured output for the advanced main agent on models that reject forcing. | ||
|
|
||
| create_agent binds tool_choice="any" on every call that carries a ToolStrategy, | ||
| and some models (Opus 5.5, Sonnet 5.5) answer any forced tool choice with a 400. | ||
| For those, the call goes out with no response_format and the output tool bound as | ||
| a plain tool on "auto", and the structured response is built here the way | ||
| create_agent builds it for a ToolStrategy (langchain.agents.factory, the | ||
| ToolStrategy branch of the model node's output handling). | ||
|
|
||
| Only that path's default behavior is mirrored: JSON-schema dict schemas, which | ||
| parse without validation, and handle_errors=True for several output calls. | ||
| """ | ||
|
|
||
| from collections.abc import Awaitable, Callable | ||
| from typing import Any | ||
|
|
||
| from langchain.agents.factory import STRUCTURED_OUTPUT_ERROR_TEMPLATE | ||
| from langchain.agents.middleware import ( | ||
| AgentMiddleware, | ||
| AgentState, | ||
| ModelRequest, | ||
| ModelResponse, | ||
| ) | ||
| from langchain.agents.structured_output import ( | ||
| MultipleStructuredOutputsError, | ||
| OutputToolBinding, | ||
| ToolStrategy, | ||
| ) | ||
| from langchain_core.messages import AIMessage, ToolCall, ToolMessage | ||
| from langchain_core.utils.function_calling import convert_to_openai_tool | ||
|
|
||
| from uipath_langchain.agent.react.no_forced_tool_choice import ( | ||
| should_skip_forced_tool_choice, | ||
| ) | ||
|
|
||
|
|
||
| def _tool_messages(calls: list[ToolCall], content: str) -> list[ToolMessage]: | ||
| return [ | ||
| ToolMessage(content=content, tool_call_id=call["id"], name=call["name"]) | ||
| for call in calls | ||
| ] | ||
|
|
||
|
|
||
| class ToolChoiceMiddleware(AgentMiddleware[AgentState[Any], Any]): | ||
| """Send a structured-output call unforced when the model rejects forcing. | ||
|
|
||
| Sits inside ``_PayloadHandlerMiddleware``, so that middleware still sees the | ||
| ToolStrategy and retries a turn that ends without the output tool. Needs the | ||
| graph to be built with the ToolStrategy: create_agent's routing and its | ||
| structured_response state come from it, and this only rewrites each call. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def _bindings( | ||
| request: ModelRequest[Any], | ||
| ) -> tuple[ToolStrategy[Any], dict[str, OutputToolBinding[Any]]] | None: | ||
| strategy = request.response_format | ||
| if not isinstance(strategy, ToolStrategy): | ||
| return None | ||
| if not should_skip_forced_tool_choice(request.model): | ||
| return None | ||
| return strategy, { | ||
| spec.name: OutputToolBinding.from_schema_spec(spec) | ||
| for spec in strategy.schema_specs | ||
| } | ||
|
|
||
| @staticmethod | ||
| def _unforced( | ||
| request: ModelRequest[Any], bindings: dict[str, OutputToolBinding[Any]] | ||
| ) -> ModelRequest[Any]: | ||
| output_tools = [convert_to_openai_tool(b.tool) for b in bindings.values()] | ||
| return request.override( | ||
| response_format=None, | ||
| tools=[*output_tools, *request.tools], | ||
| tool_choice="auto", | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _structured( | ||
| response: ModelResponse[Any], | ||
| strategy: ToolStrategy[Any], | ||
| bindings: dict[str, OutputToolBinding[Any]], | ||
| ) -> ModelResponse[Any]: | ||
| output = next( | ||
| (m for m in reversed(response.result) if isinstance(m, AIMessage)), None | ||
| ) | ||
| calls = ( | ||
| [c for c in output.tool_calls if c["name"] in bindings] if output else [] | ||
| ) | ||
| if not output or not calls: | ||
| return response | ||
| if len(calls) > 1: | ||
| error = MultipleStructuredOutputsError([c["name"] for c in calls], output) | ||
| content = STRUCTURED_OUTPUT_ERROR_TEMPLATE.format(error=str(error)) | ||
| return ModelResponse( | ||
| result=[*response.result, *_tool_messages(calls, content)] | ||
| ) | ||
| structured = bindings[calls[0]["name"]].parse(calls[0]["args"]) | ||
| content = ( | ||
| strategy.tool_message_content | ||
| or f"Returning structured response: {structured}" | ||
| ) | ||
| return ModelResponse( | ||
| result=[*response.result, *_tool_messages(calls, content)], | ||
| structured_response=structured, | ||
| ) | ||
|
|
||
| def wrap_model_call( | ||
| self, | ||
| request: ModelRequest[Any], | ||
| handler: Callable[[ModelRequest[Any]], ModelResponse[Any]], | ||
| ) -> ModelResponse[Any]: | ||
| found = self._bindings(request) | ||
| if found is None: | ||
| return handler(request) | ||
| strategy, bindings = found | ||
| response = handler(self._unforced(request, bindings)) | ||
| return self._structured(response, strategy, bindings) | ||
|
|
||
| async def awrap_model_call( | ||
| self, | ||
| request: ModelRequest[Any], | ||
| handler: Callable[[ModelRequest[Any]], Awaitable[ModelResponse[Any]]], | ||
| ) -> ModelResponse[Any]: | ||
| found = self._bindings(request) | ||
| if found is None: | ||
| return await handler(request) | ||
| strategy, bindings = found | ||
| response = await handler(self._unforced(request, bindings)) | ||
| return self._structured(response, strategy, bindings) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Leaving this as is. The only caller builds
ToolStrategy(output_json_schema)with the defaulthandle_errors=True(graph.py#L504), and for that default this sends the same message langchain does. The module docstring says only the default is mirrored (tool_choice.py#L10-L11). This is a stopgap until langchain letsToolStrategyskip the forced tool choice; if a caller needs a customhandle_errorsbefore then, I'll add it.