The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix: reuse ToolStrategy in agent factory to prevent name mismatch (#34871)
Shivangi Sharmad189663b4745
diff --git a/libs/langchain_v1/langchain/agents/factory.py b/libs/langchain_v1/langchain/agents/factory.pyindex 743d773e2b..b2032add8b 100644--- a/libs/langchain_v1/langchain/agents/factory.py+++ b/libs/langchain_v1/langchain/agents/factory.py@@ -1070,8 +1070,14 @@ def create_agent(if _supports_provider_strategy(request.model, tools=request.tools):# Model supports provider strategy - use iteffective_response_format = ProviderStrategy(schema=request.response_format.schema)- else:+ elif (+ request.response_format is initial_response_format+ and tool_strategy_for_setup is not None+ ):# Model doesn't support provider strategy - use ToolStrategy+ # Reuse the strategy from setup if possible to preserve tool names+ effective_response_format = tool_strategy_for_setup+ else:effective_response_format = ToolStrategy(schema=request.response_format.schema)else:# User explicitly specified a strategy - preserve itdiff --git a/libs/langchain_v1/tests/unit_tests/agents/test_response_format.py b/libs/langchain_v1/tests/unit_tests/agents/test_response_format.pyindex c684358f01..e75684143d 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_response_format.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_response_format.py@@ -197,6 +197,37 @@ class TestResponseFormatAsModel:assert response["structured_response"] == EXPECTED_WEATHER_DICTassert len(response["messages"]) == 5+ def test_autostrategy_with_anonymous_json_schema(self) -> None:+ """Test response_format as anonymous JSON schema (AutoStrategy).++ Verifies that tool name mismatch is avoided when using AutoStrategy with+ schemas that generate random names by ensuring the ToolStrategy instance+ is reused during execution.+ """+ anonymous_schema = {+ "type": "object",+ "properties": {+ "result": {"type": "string"},+ },+ "required": ["result"],+ }++ with patch("langchain.agents.factory._supports_provider_strategy", return_value=False):+ model = FakeToolCallingModel(tool_calls=[])+ agent = create_agent(model, [], response_format=anonymous_schema)++ # We expect a recursion error or similar because we didn't mock the tool call+ # matching our anonymous schema, but it should NOT raise ValueError+ # during the binding phase.+ try:+ agent.invoke({"messages": [HumanMessage("hi")]}, config={"recursion_limit": 1})+ except ValueError as e:+ if "which wasn't declared" in str(e):+ pytest.fail(f"Tool name mismatch occurred: {e}")+ except Exception: # noqa: S110+ # Other exceptions mean we passed the binding phase+ pass+class TestResponseFormatAsToolStrategy:def test_pydantic_model(self) -> None: