fix: allow null usage in ChatStreamChunk schema for streaming responses#220
Closed
robert-j-y wants to merge 1 commit intomainfrom
Closed
fix: allow null usage in ChatStreamChunk schema for streaming responses#220robert-j-y wants to merge 1 commit intomainfrom
robert-j-y wants to merge 1 commit intomainfrom
Conversation
The ChatStreamChunk Zod schema only accepted usage as optional (undefined), but the API sends usage: null in streaming chat completion chunks and response.created events. This caused a ZodError (invalid_type: expected object, received null) that prevented consuming streams entirely. Changes: - OpenAPI spec: add nullable: true to ChatStreamChunk.usage field - Generated schema: change usage from .optional() to .nullable().optional() - TypeScript type: update ChatUsage union to include null - Add regression test covering null, undefined, and valid usage scenarios Fixes OpenRouterTeam/ai-sdk-provider#452 (Moved from #146) Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
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
The
ChatStreamChunkZod schema rejectedusage: nullin streaming chat completion chunks, causing aZodError(invalid_type: expected object, received null) that prevented consuming streams entirely. The API sendsusage: nullin intermediate streaming chunks before final usage data is available.Fix: Changed
usagefrom.optional()to.nullable().optional()in both the OpenAPI spec and the generated schema, so it now acceptsnull | undefined | ChatUsage.The Responses API path (
OpenResponsesResult) already handledusage: nullcorrectly — only the Chat Completions streaming path was affected.Fixes OpenRouterTeam/ai-sdk-provider#452 (moved from #146)
Review & Testing Checklist for Human
allOf+nullable: truepattern) will produce the correct output when Speakeasy regenerates — this is the standard OpenAPI 3.0 nullable-ref pattern, but confirm it matches what Speakeasy expectsusagebeing strictlyChatUsage | undefined(nevernull) — e.g., code doing=== undefinedchecks instead of falsy checksNotes
src/models/chatstreamchunk.ts) was edited directly alongside the OpenAPI spec. Speakeasy's persistent edits mechanism should preserve this via 3-way merge on regeneration.StreamEventsschema already acceptedusage: null(confirmed by test) — this was only broken on the Chat Completions streaming path.Link to Devin session: https://app.devin.ai/sessions/e3b2cbc5fbfe4b7499f581cb30795525
Requested by: @robert-j-y