fix(openai): handle null choices in default response extraction#1742
Open
CodeBlackwell wants to merge 1 commit into
Open
fix(openai): handle null choices in default response extraction#1742CodeBlackwell wants to merge 1 commit into
CodeBlackwell wants to merge 1 commit into
Conversation
An OpenAI-compatible provider can return a chat/completion response with
`choices: null`. `response.get("choices", [])` returns None in that case
(the default only applies to a missing key, not an explicit null), so the
following `len(choices)` raised `TypeError: object of type 'NoneType' has
no len()` and masked the provider's real response. Fall back to [] on a
null value so the wrapper treats it as no choices.
Also guard the httpx logger handler so it is only added when none exists,
matching the comment's stated intent and avoiding duplicate log lines.
Fixes langfuse#1741
049c638 to
19cc854
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
An OpenAI-compatible provider can return a chat completion with
choices: null.response.get("choices", [])returnsNonein that case — the default only applies to a missing key, not an explicit null — so the nextlen(choices)raisesTypeError: object of type 'NoneType' has no len()and hides the provider's real response (issue #1741).This falls back to
[]whenchoicesis null, in both the chat and completion paths. Also guards the httpx logger handler so it's only added when none exists, matching the comment's intent and avoiding duplicate log lines.Added a unit test covering
choices: nullfor both resource types (fails with the reported TypeError before the fix). Fulltests/unit/test_openai.py+test_logger.pypass.Fixes #1741
Greptile Summary
This PR fixes a
TypeErrorthat occurred when an OpenAI-compatible provider returnedchoices: nullin the response body —dict.get("choices", [])does not substitute the default for an explicitnull, solen(None)raised. It also guards thehttpxlogger handler setup to avoid duplicates on module reload.langfuse/openai.py: Changesresponse.get("choices", [])toresponse.get("choices") or []in both thecompletionandchatbranches of_get_langfuse_data_from_default_response, correctly handling an explicitnullvalue.langfuse/logger.py: Wraps thehttpxconsole handler creation inif not httpx_logger.handlers:, matching the comment's intent and preventing duplicate log lines.tests/unit/test_openai.py/test_logger.py: Adds targeted unit tests for both fixes, including a regression test that would reproduce the reportedTypeErrorbefore the change.Confidence Score: 4/5
The core fix is correct and well-targeted; a single style nit in the test file is the only thing holding this back from a clean merge.
Both changes are narrow and correct. The
or []idiom properly covers the null-choices case, the logger guard prevents handler duplication, and the new tests would reproduce the original crash before the fix. The only observation is an in-function import intest_logger.pythat violates the project's import ordering rule; it does not affect test correctness but should be moved to the module level.tests/unit/test_logger.py — the
import langfuse.logger as lf_loggerstatement should move to the top of the module.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[OpenAI-compatible provider response] --> B{response is None?} B -- yes --> C[Return NoneType message] B -- no --> D[Extract model] D --> E{resource.type?} E -- completion --> F["choices = response.get('choices') or []"] E -- chat --> G["choices = response.get('choices') or []"] E -- Responses/AsyncResponses --> H[Extract output] E -- embedding --> I[Extract data] F --> J{len choices > 0?} G --> K{len choices > 0?} J -- yes, was crashing on null --> L[Extract text] J -- no or null → [] --> M[completion = None] K -- yes, was crashing on null --> N[Extract messages] K -- no or null → [] --> O[completion = None] L --> P["Return (model, completion, usage)"] M --> P N --> P O --> P H --> P I --> P%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% flowchart TD A[OpenAI-compatible provider response] --> B{response is None?} B -- yes --> C[Return NoneType message] B -- no --> D[Extract model] D --> E{resource.type?} E -- completion --> F["choices = response.get('choices') or []"] E -- chat --> G["choices = response.get('choices') or []"] E -- Responses/AsyncResponses --> H[Extract output] E -- embedding --> I[Extract data] F --> J{len choices > 0?} G --> K{len choices > 0?} J -- yes, was crashing on null --> L[Extract text] J -- no or null → [] --> M[completion = None] K -- yes, was crashing on null --> N[Extract messages] K -- no or null → [] --> O[completion = None] L --> P["Return (model, completion, usage)"] M --> P N --> P O --> P H --> P I --> PPrompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(openai): handle null choices in defa..." | Re-trigger Greptile
Context used:
Learned From
langfuse/langfuse-python#1387