Skip to content

translator: guard nil response body in OpenAI ResponseBody - #2240

Merged
nacx merged 4 commits into
envoyproxy:mainfrom
kunwarshivam:fix/translator-nil-response-body
Jun 23, 2026
Merged

translator: guard nil response body in OpenAI ResponseBody#2240
nacx merged 4 commits into
envoyproxy:mainfrom
kunwarshivam:fix/translator-nil-response-body

Conversation

@kunwarshivam

@kunwarshivam kunwarshivam commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Description

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. 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 null body 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

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>
@kunwarshivam
kunwarshivam requested a review from a team as a code owner June 16, 2026 19:17
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jun 16, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.70%. Comparing base (5a8584a) to head (787c2c9).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nacx
nacx enabled auto-merge (squash) June 23, 2026 09:09
@nacx
nacx merged commit 3456009 into envoyproxy:main Jun 23, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

translator: nil pointer panic (SIGSEGV) on null OpenAI response body

3 participants