Skip to content

Providers omitting finish_reason in streaming chunks break the SDK with ZodError. #184

@LenAnderson

Description

@LenAnderson

One example is Google as a provider for Claude Models (e.g., Sonnet 4.5).

Thrown at @openrouter/sdk/0.8.0/esm/models/operations/sendchatcompletionrequest.js:37:72

ZodError: [
  {
    "code": "invalid_union",
    "errors": [
      [
        {
          "code": "invalid_value",
          "values": [
            "tool_calls"
          ],
          "path": [],
          "message": "Invalid input: expected \"tool_calls\""
        }
      ],
      [
        {
          "code": "invalid_value",
          "values": [
            "stop"
          ],
          "path": [],
          "message": "Invalid input: expected \"stop\""
        }
      ],
      [
        {
          "code": "invalid_value",
          "values": [
            "length"
          ],
          "path": [],
          "message": "Invalid input: expected \"length\""
        }
      ],
      [
        {
          "code": "invalid_value",
          "values": [
            "content_filter"
          ],
          "path": [],
          "message": "Invalid input: expected \"content_filter\""
        }
      ],
      [
        {
          "code": "invalid_value",
          "values": [
            "error"
          ],
          "path": [],
          "message": "Invalid input: expected \"error\""
        }
      ],
      [
        {
          "expected": "string",
          "code": "invalid_type",
          "path": [],
          "message": "Invalid input: expected string, received undefined"
        }
      ]
    ],
    "path": [
      "data",
      "choices",
      0,
      "finish_reason"
    ],
    "message": "Invalid input"
  }
]

Inspecting the rawEvent at that point shows that the property is indeed missing, and adding it (just setting finish_reason to null on the chunk) before the call to parse let's the SDK accept and process the chunks without complaint.

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