Skip to content

Fix/relax schema validation#232

Merged
subtleGradient merged 7 commits intomainfrom
fix/relax-schema-validation
Nov 14, 2025
Merged

Fix/relax schema validation#232
subtleGradient merged 7 commits intomainfrom
fix/relax-schema-validation

Conversation

@subtleGradient
Copy link
Copy Markdown
Contributor

This pull request focuses on improving the robustness and forward compatibility of the API response validation by relaxing the strictness of the zod schemas. The main change is the addition of .passthrough() to all relevant zod object schemas, allowing unexpected or extra fields in API responses to be accepted without causing validation errors. This ensures that the SDK will not break if the API introduces new fields in the future. The changes also update error handling and add new tests to verify the relaxed schema behavior.

Schema Relaxation and Forward Compatibility

  • Added .passthrough() to all zod object schemas in src/chat/schemas.ts, src/completion/schemas.ts, src/schemas/error-response.ts, and src/schemas/image.ts to allow extra fields in API responses and prevent validation failures when the API returns unexpected fields. This affects chat, completion, error, and image response schemas. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

Error Handling Improvements

  • Updated error handling in src/chat/index.ts and src/completion/index.ts to consistently extract error data from API responses and pass only the relevant fields to the APICallError, improving clarity and maintainability. [1] [2]

Testing and Validation

  • Added new tests in src/chat/file-parser-schema.test.ts to verify that the relaxed schemas can parse real and extra fields in file annotations, ensuring the schemas work with current and future API responses.
  • Updated error response tests in src/schemas/error-response.test.ts to include extra metadata fields, verifying that .passthrough() works as intended. [1] [2]

Documentation

  • Added a changeset file .changeset/relax-schemas.md documenting the schema relaxation and its benefits for forward compatibility.

Plugin Configuration Example

  • Updated an end-to-end test in e2e/pdf-blob/index.test.ts to show usage of the FileParser plugin with a specific OCR engine, demonstrating plugin extensibility.

Add .passthrough() to all zod object schemas to prevent validation failures
when the API returns extra fields that aren't in our schema definitions.

This ensures the provider is forward-compatible with API changes and
doesn't break when new fields are added to responses.

Changes:
- Add .passthrough() to all object schemas in chat/schemas.ts
- Add .passthrough() to all object schemas in completion/schemas.ts
- Add .passthrough() to provider-metadata, image, and reasoning-details schemas
- Add type assertions for error handling to fix TypeScript errors from passthrough

Evidence: All 74 unit tests pass, 10/11 e2e tests pass (1 unrelated failure)
Add test to verify FileParser annotations with content arrays parse correctly.
Update error schema tests to expect passthrough fields in output.

Evidence: Test passes with file annotations containing content arrays
@subtleGradient subtleGradient self-assigned this Nov 14, 2025
Copilot AI review requested due to automatic review settings November 14, 2025 22:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the robustness and forward compatibility of API response validation by adding .passthrough() to all zod object schemas. This allows the SDK to accept extra fields from API responses that aren't explicitly defined in the schemas, preventing validation errors when the API evolves by adding new fields.

Key changes:

  • Added .passthrough() to all zod object schemas across chat, completion, error, image, reasoning, and provider metadata schemas
  • Updated error handling in chat and completion modules to properly extract and pass error data to APICallError
  • Added comprehensive tests to verify the relaxed schema behavior handles both current and extra API fields

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/schemas/reasoning-details.ts Added .passthrough() to CommonReasoningDetailSchema for forward compatibility
src/schemas/provider-metadata.ts Added .passthrough() to all nested objects in OpenRouterProviderMetadataSchema including usage details and cost objects
src/schemas/image.ts Added .passthrough() to ImageResponseSchema and nested image_url object
src/schemas/error-response.ts Added .passthrough() to OpenRouterErrorResponseSchema and nested error object
src/schemas/error-response.test.ts Updated tests to verify extra fields like metadata and user_id are preserved through .passthrough()
src/completion/schemas.ts Added .passthrough() to all nested objects in OpenRouterCompletionChunkSchema including choices, logprobs, and usage details
src/completion/index.ts Improved error handling by extracting error data to a typed variable before passing to APICallError
src/chat/schemas.ts Added .passthrough() to all nested objects in chat completion schemas including messages, tool calls, annotations, and logprobs
src/chat/index.ts Improved error handling by extracting error data to a typed variable before passing to APICallError
src/chat/file-parser-schema.test.ts Added new tests verifying the schemas correctly parse responses with extra API fields like native_finish_reason and refusal
e2e/pdf-blob/index.test.ts Added plugin configuration example demonstrating the FileParser plugin with OCR engine specification
.changeset/relax-schemas.md Added changeset documenting the schema relaxation changes
Comments suppressed due to low confidence (1)

src/schemas/reasoning-details.ts:24

  • Using .extend() with .shape on a schema that has .passthrough() applied will lose the passthrough behavior. When you use .shape, it extracts only the defined fields and discards modifiers like .passthrough(). The resulting extended schemas (ReasoningDetailSummarySchema, ReasoningDetailEncryptedSchema, ReasoningDetailTextSchema) will not allow extra fields, defeating the purpose of this PR. Instead, use .merge() or reapply .passthrough() after extending.
export const ReasoningDetailSummarySchema = z
  .object({
    type: z.literal(ReasoningDetailType.Summary),
    summary: z.string(),
  })
  .extend(CommonReasoningDetailSchema.shape);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@subtleGradient subtleGradient merged commit 2b49df4 into main Nov 14, 2025
3 checks passed
@subtleGradient subtleGradient deleted the fix/relax-schema-validation branch November 14, 2025 22:35
@github-actions github-actions bot mentioned this pull request Nov 14, 2025
kesavan-byte pushed a commit to osm-API/ai-sdk-provider that referenced this pull request Feb 13, 2026
* fix: relax zod schemas with passthrough to allow unexpected fields

Add .passthrough() to all zod object schemas to prevent validation failures
when the API returns extra fields that aren't in our schema definitions.

This ensures the provider is forward-compatible with API changes and
doesn't break when new fields are added to responses.

Changes:
- Add .passthrough() to all object schemas in chat/schemas.ts
- Add .passthrough() to all object schemas in completion/schemas.ts
- Add .passthrough() to provider-metadata, image, and reasoning-details schemas
- Add type assertions for error handling to fix TypeScript errors from passthrough

Evidence: All 74 unit tests pass, 10/11 e2e tests pass (1 unrelated failure)

* test: add file parser annotation schema test

Add test to verify FileParser annotations with content arrays parse correctly.
Update error schema tests to expect passthrough fields in output.

Evidence: Test passes with file annotations containing content arrays

* chore: add changeset for schema relaxation

* Add FileParserPlugin with Mistral OCR to PDF test

* Format long lines in file-parser-schema.test.ts

* Replace provider name with generic example in tests

* Replace type cast with ts-expect-error
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.

2 participants