Replace manual JSON parsing with Instructor for structured output reliability#339
Open
pegahmansourian wants to merge 1 commit into
Conversation
…iability
- Add llm_structured() and llm_astructured() in utils.py using instructor.from_litellm()
- Add Pydantic models for all 11 structured outputs in page_index.py
- Replace all extract_json() calls with typed Instructor calls
- Remove 'Directly return the final JSON structure' from all prompts
- Fixes silent KeyError crashes when extract_json() returned {} on malformed LLM output
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.
Problem
The codebase uses manual JSON prompt instructions across 11 locations in
page_index.py, with a customextract_json()parser inutils.pythathandles malformed output through multiple fallback strategies.
When the LLM returns slightly malformed JSON,
extract_json()silentlyreturns
{}, causing downstreamKeyErrorcrashes like:Solution
Replace manual JSON parsing with Instructor,
which is patched directly onto the existing LiteLLM client already used in the codebase.
Changes
utils.pysync_instructor_clientandasync_instructor_clientpatched viainstructor.from_litellm()llm_structured()for sync structured callsllm_astructured()for async structured callsextract_json()andget_json_content()can be removed once all callers are updatedpage_index.pyextract_json()calls withllm_structured()/llm_astructured()"Directly return the final JSON structure"from all prompts — Instructor handles this automaticallyBenefits
{}returnsLiteral["yes", "no"]fields are always validinstructoris the only new dependencyNew dependency
This PR adds one new dependency:
instructorAdd to
requirements.txt:instructorOr install directly: