Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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.shapeon 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.
Merged
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
.passthrough()to all zod object schemas insrc/chat/schemas.ts,src/completion/schemas.ts,src/schemas/error-response.ts, andsrc/schemas/image.tsto 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
src/chat/index.tsandsrc/completion/index.tsto consistently extract error data from API responses and pass only the relevant fields to theAPICallError, improving clarity and maintainability. [1] [2]Testing and Validation
src/chat/file-parser-schema.test.tsto verify that the relaxed schemas can parse real and extra fields in file annotations, ensuring the schemas work with current and future API responses.src/schemas/error-response.test.tsto include extra metadata fields, verifying that.passthrough()works as intended. [1] [2]Documentation
.changeset/relax-schemas.mddocumenting the schema relaxation and its benefits for forward compatibility.Plugin Configuration Example
e2e/pdf-blob/index.test.tsto show usage of the FileParser plugin with a specific OCR engine, demonstrating plugin extensibility.