fix(openai): Improve vLLM API compatibility and error messages #33662
+252
−15
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.
Description
Fixes #32252 - Resolves the issue where LangChain-OpenAI throws
TypeError: Received response with null value for choiceswhen using vLLM's OpenAI-compatible API.Problem
When using vLLM's OpenAI-compatible API through LangChain, the response parsing would fail with a null choices error, even though the
choicesfield exists in the raw response. This was caused by how the OpenAI SDK'smodel_dump()method handles non-OpenAI API responses.Solution
This PR improves response handling in the
_create_chat_resultmethod:model_dump_json()ifmodel_dump()failsChanges
langchain_openai/chat_models/base.py:tests/unit_tests/chat_models/test_base.py:test_vllm_response_with_valid_choices()- Verifies vLLM responses worktest_improved_null_choices_error_message()- Validates error message improvementsTesting
All existing tests pass (279 passed, 4 xfailed, 1 xpassed).
New tests verify:
Usage Example
After this fix, users can successfully use vLLM endpoints:
Resolves #32252