Skip to content

The agent engineering platform.

fix(langchain, openai): backward compat for response_format (#33945)

ccurme3d415441e88e
diff --git a/libs/langchain_v1/langchain/agents/factory.py b/libs/langchain_v1/langchain/agents/factory.py
index 87e02e6026..0d136da06a 100644
--- a/libs/langchain_v1/langchain/agents/factory.py
+++ b/libs/langchain_v1/langchain/agents/factory.py
@@ -1009,9 +1009,8 @@ def create_agent( # noqa: PLR0915
# Bind model based on effective response format
if isinstance(effective_response_format, ProviderStrategy):
- kwargs: dict[str, Any] = {
- "response_format": effective_response_format.schema_spec.json_schema
- }
+ # (Backward compatibility) Use OpenAI format structured output
+ kwargs = effective_response_format.to_model_kwargs()
return (
request.model.bind_tools(
final_tools, strict=True, **kwargs, **request.model_settings
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py
index e567893cb3..166d8c1e62 100644
--- a/libs/partners/openai/langchain_openai/chat_models/base.py
+++ b/libs/partners/openai/langchain_openai/chat_models/base.py
@@ -1844,6 +1844,14 @@ class BaseChatOpenAI(BaseChatModel):
kwargs["tool_choice"] = tool_choice
if response_format:
+ if (
+ isinstance(response_format, dict)
+ and response_format.get("type") == "json_schema"
+ and "schema" in response_format.get("json_schema", {})
+ ):
+ # compat with langchain.agents.create_agent response_format, which is
+ # an approximation of OpenAI format
+ response_format = cast(dict, response_format["json_schema"]["schema"])
kwargs["response_format"] = _convert_to_openai_response_format(
response_format
)