Skip to content

Conversation

@bledden
Copy link

@bledden bledden commented Dec 21, 2025

Summary

This PR fixes an issue where the SDK rejects file content items (type: 'file') with a Zod validation error before requests ever reach the OpenRouter API. Users couldn't send PDFs or other documents using the documented format.

The problem: The ChatMessageContentItem type only included text, image, audio, and video variants. When users tried to send file content as documented, the SDK's internal validation failed immediately.

The fix: Added the missing ChatMessageContentItemFile type that matches what the OpenRouter API actually accepts.

Changes

  • Added ChatMessageContentItemFile schema to the OpenAPI spec
  • Created new TypeScript file with types and Zod validation schemas
  • Updated ChatMessageContentItem union to include the file variant
  • Added E2E test that confirms PDF uploads work correctly

Testing

The fix was validated by:

  1. Testing the raw API with curl to confirm type: "file" is accepted
  2. Building the TypeScript project (no errors)
  3. Running the new E2E test that uploads a PDF and verifies the response
  4. Running all existing tests to ensure nothing broke

Example usage

const response = await client.chat.send({
  model: 'anthropic/claude-sonnet-4',
  messages: [{
    role: 'user',
    content: [
      { type: 'text', text: 'What is this document about?' },
      {
        type: 'file',
        file: {
          filename: 'document.pdf',
          fileData: 'https://example.com/document.pdf'
        }
      }
    ]
  }]
});

Fixes #83

The SDK was rejecting file content items with a Zod validation error
before requests ever reached the OpenRouter API. This happened because
the ChatMessageContentItem union type didn't include a "file" variant,
even though the API accepts it.

This change adds:
- ChatMessageContentItemFile schema to the OpenAPI spec
- New TypeScript types and Zod schemas for file content
- Export from the models index
- E2E test confirming PDF uploads work correctly

The file content type supports:
- filename (required)
- file_data (URL or base64-encoded content)
- file_id (for previously uploaded files)

Fixes OpenRouterTeam#83
@bledden
Copy link
Author

bledden commented Dec 21, 2025

Schema verification: I confirmed the file content type schema against the OpenRouter PDF documentation.

The documented format is:

{
  "type": "file",
  "file": {
    "filename": "document.pdf",
    "file_data": "https://example.com/document.pdf"
  }
}

The implementation matches this exactly. I also included an optional file_id field based on the ResponseInputFile schema in the OpenAPI spec, for potential future file reference support.

The TypeScript SDK repo does not accept direct changes to generated code.
Only the OpenAPI spec change and E2E test should be included.
The maintainers will regenerate the SDK after merging.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant