translator: guard nil response body in OpenAI ResponseBody - #2240
Merged
nacx merged 4 commits intoJun 23, 2026
Conversation
A JSON `null` response body decodes into a nil `*ChatCompletionResponse` without an error, because the decode target is a `**ChatCompletionResponse` (`Decode(&resp)`). Some OpenAI-compatible backends return a `null` body with a 200 status (e.g. an upstream that aborts the response after committing headers under load). In the non-streaming path `ResponseBody` then dereferenced the nil response at `resp.Usage.PromptTokens`, panicking the ext-proc with a SIGSEGV (addr 0x80). `RedactBody` already guards `resp == nil`, so the nil case is known; the main path just missed it. Guard it by treating a nil response as an empty response: report zero token usage and fall back to the request model, mirroring the streaming path's behavior when no usage is present, rather than crashing. Adds a regression test that feeds a literal `null` body and asserts no panic and zero usage. Signed-off-by: Kunwar Srivastav <kshivamsrivastav@gmail.com>
nacx
approved these changes
Jun 23, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2240 +/- ##
=======================================
Coverage 84.69% 84.70%
=======================================
Files 144 144
Lines 21248 21250 +2
=======================================
+ Hits 17997 17999 +2
Misses 2166 2166
Partials 1085 1085 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
nacx
enabled auto-merge (squash)
June 23, 2026 09:09
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.
Description
A JSON
nullresponse body decodes into a nil*ChatCompletionResponsewithoutan error, because the decode target is a
**ChatCompletionResponse(Decode(&resp)).Some OpenAI-compatible backends return a
nullbody with a 200 status (e.g. anupstream that aborts the response after committing headers under load). In the
non-streaming path
ResponseBodythen dereferenced the nil response atresp.Usage.PromptTokens, panicking the ext-proc with a SIGSEGV (addr 0x80).RedactBodyalready guardsresp == nil, so the nil case is known; the main pathjust missed it. This treats a nil response as an empty response: report zero token
usage and fall back to the request model, mirroring the streaming path's behavior
when no usage is present, rather than crashing.
A regression test feeds a literal
nullbody and asserts no panic and zero usage.Per the project's generative-AI policy: this change was prepared with the
assistance of an AI coding tool (Claude Code). The submitter understands and takes
full ownership of the change.
Related Issues/PRs (if applicable)
Fixes #2273
Special notes for reviewers (if applicable)
N/A