Skip to content

Streaming Responses API fails Zod validation - usage field is null instead of object/undefined #452

@mattapperson

Description

@mattapperson

Moved from OpenRouterTeam/typescript-sdk#146

Description

When using the Responses API with streaming, the response.created event fails Zod validation because the usage field is null, but the SDK schema only allows object | undefined, not null.

SDK Version

@openrouter/sdk@0.3.12

Steps to Reproduce

  1. Make a streaming request to the Responses API
  2. The first event (response.created) triggers a ZodError
import { OpenRouter } from "@openrouter/sdk";

const client = new OpenRouter();

const response = client.callModel({
    model: 'anthropic/claude-sonnet-4.5',
    input: 'Hello',
});

for await (const event of response.getTextStream()) {
    // Throws ZodError on first event
    console.log(event);
}

Error

ZodError invalid_union
ZodError: [
  {
    "code": "invalid_union",
    "errors": [
      [
        {
          "expected": "object",
          "code": "invalid_type",
          "path": [
            "response",
            "usage"
          ],
          "message": "Invalid input: expected object, received null"
        }
      ],
      ...
    ]
  }
]

Expected Behavior

The SDK should handle null values for usage in streaming events, since initial events like response.created don't have usage data yet.

Workaround

None currently — the error prevents consuming the stream entirely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions