refactor(types): replace json-schema library with custom JSONSchema type#220
Merged
refactor(types): replace json-schema library with custom JSONSchema type#220
Conversation
- Define comprehensive JSONSchema interface in types.ts covering all JSON Schema draft-07 properties - Add toJsonSchema() method to BaseTool and Tools classes for framework-agnostic schema export - Refactor toOpenAI(), toAnthropic(), and toOpenAIResponses() to use toJsonSchema() internally, reducing code duplication - Use type-fest OverrideProperties for ObjectJSONSchema type to ensure type: 'object' is always set - Remove json-schema and @types/json-schema dependencies This reduces external dependencies while providing a more flexible JSONSchema type that works seamlessly with OpenAI, Anthropic, and other LLM providers.
commit: |
Consolidate schema generation by reusing toJsonSchema() instead of manually constructing the schema object. This reduces duplication and ensures consistency across all conversion methods.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes external JSON Schema dependencies (json-schema and @types/json-schema) in favor of a custom JSONSchema interface, reducing external dependencies while maintaining full compatibility with OpenAI, Anthropic, and other LLM providers.
Key Changes:
- Introduced a comprehensive custom
JSONSchemainterface covering all JSON Schema draft-07 properties - Added
toJsonSchema()methods toBaseToolandToolsclasses for framework-agnostic schema export - Refactored existing methods (
toOpenAI(),toAnthropic(),toOpenAIResponses()) to use the newtoJsonSchema()internally
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/types.ts | Defines custom JSONSchema interface with all draft-07 properties and updates type references |
| src/tool.ts | Adds ObjectJSONSchema type and toJsonSchema() methods; refactors conversion methods to use it |
| src/tool.test.ts | Updates type imports and test assertions from JSONSchema7 to custom JSONSchema |
| pnpm-workspace.yaml | Removes json-schema and @types/json-schema from catalog dependencies |
| pnpm-lock.yaml | Removes dependency resolution entries for json-schema and @types/json-schema |
| package.json | Removes json-schema and @types/json-schema from dependencies and devDependencies |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Import JSONSchema7 type from @ai-sdk/provider as AISDKJSONSchema - Use satisfies AISDKJSONSchema to validate schema at compile time - Move jsonSchema type import to top-level for cleaner code - Add @ai-sdk/provider as dev dependency for type checking
- Create tryImport() helper function for dynamic imports with friendly error messages when optional dependencies are not installed - Refactor toAISDK() to use tryImport() for cleaner code - Remove unused jsonSchema type import from top-level
- Test successful import of existing modules - Test StackOneError is thrown for non-existent modules - Verify error message includes module name and install hint
3 tasks
- Remove deprecated v4 parameters property - Use satisfies for type-safe tool definition - Remove outdated TODO comment about ts-ignore - Simplify tool definition by constructing all properties upfront
This reverts commit 91d6c79.
468aea6 to
fafd7ff
Compare
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.
Summary
JSONSchemainterface intypes.tscovering all JSON Schema draft-07 propertiestoJsonSchema()method toBaseToolandToolsclasses for framework-agnostic schema exporttoOpenAI(),toAnthropic(), andtoOpenAIResponses()to usetoJsonSchema()internallyjson-schemaand@types/json-schemadependenciesWhy
This reduces external dependencies while providing a more flexible JSONSchema type that works seamlessly with OpenAI, Anthropic, and other LLM providers. The custom type also uses
type-fest'sOverridePropertiesfor cleaner type narrowing (ObjectJSONSchema).The
toJsonSchema()method enables integration with any framework that accepts standard JSON Schema for tool definitions, such as:Test plan