Skip to content

Upload - SDK - Zod invalid_union error before it ever reaches the OpenRouter API #83

Closed as not planned

Description

@stayingcool

Problem
When using @openrouter/sdk to send a chat request with a file content item (as shown in the official documentation for PDF processing), the SDK rejects the request with a SDKValidationError / Zod invalid_union error before it ever reaches the OpenRouter API.

Concretely, this payload:

await openRouter.chat.send({
  model: 'anthropic/claude-sonnet-4',
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'What are the main points in this document?' },
        {
          type: 'file',
          file: {
            filename: 'document.pdf',
            fileData: 'https://bitcoin.org/bitcoin.pdf',
          },
        },
      ],
    },
  ],
  plugins: [
    {
      id: 'file-parser',
      pdf: { engine: 'mistral-ocr' },
    },
  ],
});

fails with a Zod validation error on messages[0].content[1].type: "file".

Root Cause
The installed SDK’s runtime schema and TypeScript types do not match the documented API:

  • In node_modules/@openrouter/sdk/esm/models/chatmessagecontentitem.d.ts:
  export type ChatMessageContentItem =
    (ChatMessageContentItemText  & { type: "text" })      |
    (ChatMessageContentItemImage & { type: "image_url" }) |
    (ChatMessageContentItemAudio & { type: "input_audio" }) |
    (ChatMessageContentItemVideo & { type: "input_video" });

There is no variant for type: "file".

The corresponding Zod schema used internally by chat.send enforces this union at runtime. As a result, any content item with type: "file" is considered invalid, even if it matches the structure shown in the official docs.

In short: the SDK’s validation layer and type definitions lag behind the documented feature (PDF/file support via type: 'file' + file: { filename, fileData }), so all such requests are rejected client‑side by the SDK before reaching OpenRouter.

Activity

  1. stayingcool commented on Nov 15, 2025

    @stayingcool
    Author

    I'm using:

    "@openrouter/sdk": "^0.1.11",

  2. mattapperson commented on Nov 15, 2025

    @mattapperson
    Contributor

    Thanks for the report and I apologize for the inconvenience. I’ll get a new build out with a fix asap

  3. added a commit that references this issue on Dec 21, 2025
    607556f
  4. z3ugma commented on Jan 27, 2026

    @z3ugma

    I am also running into this, what is the status on the PR #121 getting merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugsbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions