fix(langchain-openai): fix operator precedence in reasoning initialization #9664
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.
Fixes #9663
Description
This PR fixes a bug in the ChatOpenAI constructor where the
reasoningparameter was incorrectly overridden due to an operator precedence issue.Problem
The original initialization logic was:
When
fields.reasoningis provided, the expressionfields?.reasoning ?? fields?.reasoningEffortevaluates to thereasoningobject (truthy). Consequently, the ternary operator executes the true branch:{ effort: fields.reasoningEffort }.However, since the user provided
reasoning,fields.reasoningEffortis typicallyundefined. This results in thereasoningfield 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
reasoningEffortifreasoningis null or 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
reasoningEffortcontinues to work as expected.