Skip to content

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

@stayingcool

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.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions