Skip to content

Conversation

@wookingwoo
Copy link

@wookingwoo wookingwoo commented Dec 15, 2025

Fixes #9663

Description

This PR fixes a bug in the ChatOpenAI constructor where the reasoning parameter was incorrectly overridden due to an operator precedence issue.

Problem

The original initialization logic was:

this.reasoning =
    fields?.reasoning ?? fields?.reasoningEffort
        ? { effort: fields.reasoningEffort }
        : undefined;

When fields.reasoning is provided, the expression fields?.reasoning ?? fields?.reasoningEffort evaluates to the reasoning object (truthy). Consequently, the ternary operator executes the true branch: { effort: fields.reasoningEffort }.
However, since the user provided reasoningfields.reasoningEffort is typically undefined. This results in the reasoning field being set to { effort: undefined }, effectively discarding the user's input.

Solution

I have added parentheses to correctly group the fallback logic. The code now only checks reasoningEffort if reasoning is null or undefined.

this.reasoning=
fields?.reasoning??
  (fields?.reasoningEffort
? {effort:fields.reasoningEffort }
:undefined);

Related Issues

Additional Context

This is a one-line fix in the v0.3 branch that restores critical functionality for GPT-5 and o-series models. The fix maintains full backward compatibility - existing code using reasoningEffort continues to work as expected.

@changeset-bot
Copy link

changeset-bot bot commented Dec 15, 2025

⚠️ No Changeset found

Latest commit: 490bf47

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

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

Can we add a test for this so we don't regress?

@wookingwoo
Copy link
Author

Can we add a test for this so we don't regress?

Great suggestion! I've added tests for this - see the "reasoning field initialization" test suite.

We can run just these tests with:

cd libs/langchain-openai
yarn test chat_models.test.ts -t "reasoning field initialization"

@wookingwoo wookingwoo force-pushed the fix/openai-reasoning-effort-override-bug branch from 7b88e69 to 2b2ba3d Compare December 16, 2025 08:15
@wookingwoo wookingwoo force-pushed the fix/openai-reasoning-effort-override-bug branch from 2b2ba3d to 490bf47 Compare December 16, 2025 08:23
@wookingwoo
Copy link
Author

@christian-bromann Tests are added and the fix is ready.
I’d appreciate a final review/approval when you get a chance. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants