Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(prompts): validate jsonschema using third-party library #5988

Merged
merged 6 commits into from
Jan 10, 2025

Conversation

axiomofjoy
Copy link
Contributor

@axiomofjoy axiomofjoy commented Jan 10, 2025

LLM API providers support relatively advanced features of JSON schema for tools and response formats (e.g., recursive schemas) that are complex to implement in Pydantic. So this PR simplifies things and introduces a third-party library.

There are two main choices for third-party libraries in the Python ecosystem, jsonschema and fastjsonschema. As the name suggests, the latter is significantly faster (>10x in my benchmarking), but seems to be less actively maintained (no support for more recent formats and less overall activity in the repo). It also has a few quirks in implementation that deviate from the standard spec and make it more lax than other implementations in a way that could make it difficult to migrate to jsonschema. So I chose jsonschema to start.

I am relying on JSON Schema draft 7, a relatively old version of the spec that is also supported in fastjsonschema to give us flexibility to switch (in theory). It also fully supports all the code snippets I've found in OpenAI's and Anthropic's documentation so far. The implementation is open to adding support for additional versions.

I've added a max version to the jsonschema version so that we are not on the bleeding edge in case there is a regression.

resolves #5987

@dosubot dosubot bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jan 10, 2025
@axiomofjoy axiomofjoy changed the base branch from main to prompts January 10, 2025 08:57
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Jan 10, 2025
@axiomofjoy axiomofjoy merged commit d82d609 into prompts Jan 10, 2025
49 checks passed
@axiomofjoy axiomofjoy deleted the xander/jsonschema branch January 10, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[prompts] refactor to use third-party library to validate jsonschema fields
2 participants