Skip to content

Conversation

@thdesc
Copy link
Contributor

@thdesc thdesc commented Dec 4, 2025

  • Ensure ChatOpenAI.bind_tools forwards its strict flag into _convert_to_openai_response_format(...), so the resulting response_format includes OpenAI’s "strict": true setting when requested.
  • With this change, langchain.agents.create_agent can now drive OpenAI’s Structured Output mode purely by providing a Pydantic response_format: the factory already passes strict=True when it selects ProviderStrategy (see here) and that flag now propagates all the way to the OpenAI API.

Code to reproduce:

from pydantic import BaseModel, Field, ConfigDict

from langchain.agents import create_agent


class ProductRating(BaseModel):
    rating: int = Field(..., description="Rating from 1-5", ge=1, le=5)
    comment: str = Field(description="Review comment")
    model_config = ConfigDict(extra="forbid")

agent = create_agent(
    model="gpt-5-mini",
    tools=[],
    response_format=ProductRating,
    system_prompt="You are a helpful assistant that parses product reviews. Do not make any field or value up.",
)

response = agent.invoke({
    "messages": [{"role": "user", "content": "Toto"}]
})

This could raise an error because the structured output is not forced.

With this PR, the structured output is correctly set and no error will be raised with the provided code.

@thdesc thdesc requested review from ccurme and mdrxy as code owners December 4, 2025 14:54
@github-actions github-actions bot added integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs fix For PRs that implement a fix labels Dec 4, 2025
@thdesc
Copy link
Contributor Author

thdesc commented Dec 4, 2025

This PR could solve this issue #34146.

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 5, 2025

CodSpeed Performance Report

Merging #34205 will not alter performance

Comparing thdesc:fix/strict_flag (5c9ea0f) with master (3ace4e3)

Summary

✅ 6 untouched
⏩ 28 skipped1

Footnotes

  1. 28 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@ccurme ccurme self-assigned this Dec 5, 2025
Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for submitting this!

We can do this without a breaking change (turning on strict mode for users when it was previously disabled) by supporting

ProviderStrategy(schema, strict=True)

as is done here: #34149

Closing in favor of that approach. We'll plan to release this in langchain 1.2.

@ccurme ccurme closed this Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix For PRs that implement a fix integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants