fix(openai): resolve vLLM compatibility issue with ChatOpenAI (#32252) #1
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.
Overview
This PR fixes the vLLM compatibility issue reported in langchain-ai#32252 where users encountered a
TypeError: "Received response with null value for 'choices'"
when using vLLM with LangChain-OpenAI integration.Problem Description
The issue occurred because some OpenAI-compatible APIs like vLLM return valid response objects, but the OpenAI client library's
model_dump()
method sometimes fails to properly serialize thechoices
field, returningNone
instead of the actual choices array. This caused the_create_chat_result
method inBaseChatOpenAI
to raise a TypeError.Solution
Added a fallback mechanism in the
_create_chat_result
method that:response.model_dump()
as beforemodel_dump()
returnschoices: None
, attempts to access choices directly from the response object viaresponse.choices
model_dump()
or__dict__
fallbackChanges Made
Core Fix
libs/partners/openai/langchain_openai/chat_models/base.py
_create_chat_result()
(lines ~1208-1230)model_dump()
returnschoices: None
Test Coverage
libs/partners/openai/tests/unit_tests/chat_models/test_vllm_compatibility.py
(new)model_dump()
fails butresponse.choices
worksmodel_dump()
Testing
Code Quality
ruff format
andruff check
Backward Compatibility
Related Issues
Review Checklist