feat(pydantic): add ToonPydanticModel with schema_to_toon() and from_toon() #46
+131
−0
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.
Description
Adds first-class Pydantic integration via an optional [
pydantic] extra. Users can install withpip install "toon-python[pydantic]"and inherit fromToonPydanticModelto get.schema_to_toon()(ultra-compact schema for prompts) and.from_toon()(parse raw TOON output directly into a validated model). Saves 50–60% tokens on schemas and eliminates JSON parsing errors completely.Key methods:
schema_to_toon()- Convert Pydantic schema to compact TOON formatfrom_toon()- Parse LLM TOON output into validated model instancesType of Change
Related Issues
Part of #44 (Agent framework integration – Pydantic AI request)
Changes Made
src/toon_format/pydantic/withToonPydanticModelmixintests/test_pydantic.pywith 4 comprehensive tests[pydantic]extra inpyproject.tomlREADME.mdwith clear installation + usage examplesSPEC Compliance
(Uses existing
encode/decode– 100 % spec-compliant)Testing
Test Output
Code Quality
ruff check src/toon_format tests– no issuesruff format src/toon_format tests– code formattedmypy src/toon_format– no critical errorspytest tests/ -vChecklist
Performance Impact
Breaking Changes
Screenshots / Examples
Additional Context
This + the already-open LangChain PR completes the two most requested agent-framework integrations from discussion #44.
TOON is now the cheapest, most reliable format for structured LLM output in 2025 – works perfectly with Instructor, Outlines, Marvin, LangChain agents, etc.
Fully optional – core users completely unaffected.
Thank you for the quick review – let’s make TOON the default for AI engineering!