fix(openai): handle top-level code/param on Responses WS error frames#2024
Merged
Conversation
🦋 Changeset detectedLatest commit: dff5d41 The changes in this PR will be included in the next version bump. This PR includes changesets to release 36 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The nullish-field replacer was preventive hardening with no live bug in JS (unlike Python, unset optional fields are never serialized as null). Keep only the error-frame fix: accept top-level code/param on Responses WS error frames so error-code routing works for top-level codes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
davidzhao
approved these changes
Jul 11, 2026
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.
Summary
response.createrequests, matching SDK behavior for unset optional fields.codeandparamfields so request-validation protocol errors surface cleanly.Source diff coverage
Classification
livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/responses/llm.pyplugins/openai/src/ws/llm.ts,plugins/openai/src/ws/types.tsws/llm.tsand schemas intows/types.ts, so the serializer fix was adapted toJSON.stringifywith a nullish-field replacer and the error-frame parsing fix was adapted by accepting top-levelcode/paramand consulting top-levelcodein the handler.tests/test_plugin_openai_responses.pyplugins/openai/src/responses/llm.test.tsVerification
pnpm build --filter @livekit/agents-plugin-openai...passed.pnpm lint --filter @livekit/agents-plugin-openai...passed with existing warnings.pnpm test -- plugins/openai/src/responses/llm.test.ts plugins/openai/src/ws/llm.test.tspassed.pnpm test -- plugins/openairan the full OpenAI plugin suite; 5 files passed and 2 existing suites failed outside this port.plugins/openai/src/stt.test.tsfails loadingplugins/silero/dist/silero_vad.onnxwithProtobuf parsing failed.plugins/openai/src/tts.test.tsfails becauseSTTnow requires a VAD forgpt-realtime-whisper.Ported from livekit/agents#6385
Original PR description
Summary
The OpenAI Responses WebSocket transport (
use_websocket=True, the default forresponses.LLM) serializes request models by hand —json.dumps(msg, default=lambda o: o.model_dump(mode="json"))— instead of going through the openai SDK. The SDK omits unset fields; this hand-rolled path did not (exclude_nonewas missing), so every Optional field that defaults toNonewas emitted as an explicitnullon the wire.That is fine until the OpenAI schema grows an Optional enum field.
openai-python2.45.0 addedReasoning.mode(Literal["standard","pro"] | None, defaultNone). The plugin auto-attachesReasoning(effort="none")for gpt-5.x models, which now serializes as:and the Responses API rejects it with HTTP 400:
Worse, that 400 was invisible: the error-frame parser crashed while trying to report it (see fix #2), so users saw a misleading pydantic
ValidationErroraboutsequence_numberinstead of the real message.This breaks any Responses-API user on the WS transport running openai ≥ 2.45.0 (gpt-5.x, where
reasoningis auto-set). The HTTP transport (use_websocket=False) is unaffected because the SDK omits unset fields.What changed
Two small fixes in
responses/llm.py, plus a hermetic regression test.Serialization —
_ResponsesWebsocket.generate_response._defaultnow usesmodel_dump(mode="json", exclude_none=True). This mirrors the SDK's on-the-wire shape and is forward-compatible with any future Optional field OpenAI adds to these models. (Matches the file's own existing convention —_handle_response_completedalready usesexclude_none=True.)Error reporting —
_parse_ws_eventdefaultssequence_numberwhen validating top-levelerrorframes. Those frames (e.g. a request-validation 400) don't carrysequence_number, whichResponseErrorEventmarks required, so validating them raised aValidationErrorthat masked the real API message. With the default, genuine errors surface cleanly as anAPIStatusErrorvia_handle_error.Test —
tests/test_plugin_openai_responses.py, hermetic (no network/keys):generate_responsethrough a fake WS pool and asserts the serializedreasoninghas no null-valued keys;sequence_number-less error frame parses cleanly and preserves the message.What this changes for users
Before: on openai ≥ 2.45.0, a gpt-5.x agent using the Responses API over WebSocket (the default) fails every turn with an opaque
ValidationErroraboutsequence_number; the underlying cause (a 400 onreasoning.mode) is hidden.After: the request serializes the way the SDK would, the turn succeeds, and if any request is rejected the real API error message is surfaced instead of a parser crash.
Testing
uv run pytest tests/test_plugin_openai_responses.py --plugin openai→ 2 passed.Reasoning(effort="none")now returnsresponse.completed(400 before the fix); a deliberately invalid request surfaces the real error message (pydantic crash before the fix).ruff format --checkandruff checkpass on the changed file.Notes