feat: schema complexity analyzer + automatic mode selection#2394
feat: schema complexity analyzer + automatic mode selection#2394mimran-khan wants to merge 1 commit into
Conversation
|
Reviewed for merge readiness. I would keep this open for #2393, but not merge it yet: it adds new public API ( |
Introduce two complementary utilities that help users get better extraction results on the first attempt: 1. `analyze_schema(response_model)` inspects a Pydantic model's JSON schema and reports complexity metrics (depth, field count, recursion, large enums) along with actionable findings and a 0-100 complexity score. 2. `select_mode(response_model, provider)` picks the optimal extraction mode for a given provider by cross-referencing schema complexity with provider capabilities. Handles recursive schemas, deep nesting, and high-complexity patterns by routing to the mode most likely to succeed. Both are exposed at `instructor.analyze_schema` and `instructor.select_mode` for convenient top-level access.
8972db1 to
bc55f4f
Compare
|
Thanks for the feedback. I've addressed both points:
Branch is force-pushed so CI should pick it up fresh. |
What this does
I noticed that picking the right mode is a common friction point, especially for users with complex nested schemas. This PR adds two utilities that solve this at the library level:
analyze_schema(response_model)- inspects a Pydantic model before any API call and reports:select_mode(response_model, provider)- picks the optimal extraction mode by cross-referencing schema complexity against provider capabilities:Why
I ran into this myself while testing extraction on deeply nested models. The retry loop burned tokens because the schema was too complex for TOOLS mode, but there was no guidance about that. Checking the issues list, I found similar confusion across multiple discussions.
These utilities are pure functions with no side effects, no network calls, and zero additional dependencies. They are lazy-loaded so they don't affect import performance.
Files changed
instructor/v2/core/schema_analyzer.py- the complexity analysis engineinstructor/v2/core/auto_mode.py- mode selection logic using provider specsinstructor/__init__.py- top-level exports (analyze_schema, SchemaAnalysis, select_mode)instructor/v2/core/__init__.py- v2 core exportstests/test_schema_analyzer.py- 28 tests for the analyzertests/test_auto_mode.py- 31 tests for mode selectionUsage
Test results
All 59 new tests pass. Broader suite (1537 tests) has no regressions.
Closes #2393
Checklist before requesting a review