fix: resolve 5 critical architectural issues (race condition, name-based PDF fill, AppError handler, OLLAMA_MODEL env var)#193
Open
Gopisokk wants to merge 2 commits intofireform-core:mainfrom
Conversation
- fix(filler): replace fragile index-based PDF filling with name-based matching Field names from annot.T are now decoded and looked up directly in the LLM answers dict (case-insensitive). Plural list values are joined with '; '. Unmatched fields are logged as warnings instead of silently filling wrong fields. - fix(file_manipulator): eliminate race condition from shared mutable LLM state Removed the shared self.llm instance. A fresh LLM(transcript_text, target_fields) is now created per-request inside fill_form() to guarantee full isolation under concurrent requests. Also replaced silent 'return None' with FileNotFoundError. - fix(forms): remove duplicate get_template() DB call (N+1) The template was fetched twice per request; now fetched once, stored, and reused. FileNotFoundError from FileManipulator is caught and converted to 422 AppError. - fix(main): register AppError exception handler register_exception_handlers(app) was defined but never called, causing AppError raises (e.g. 404 Template not found) to produce unhandled 500 crashes instead of clean JSON error responses.
The model name was hardcoded to 'mistral' on line 57, meaning any operator
who wanted to use llama3, phi3, or another model had to edit source code.
Changes:
- Read model from os.getenv('OLLAMA_MODEL', 'mistral') — backward compatible,
defaults to 'mistral' when env var is not set
- Move ollama_host, ollama_url, and ollama_model resolution outside the per-field
loop — they are constant for the whole request and were being re-evaluated on
every iteration unnecessarily
- Remove stale commented-out dead code (old hardcoded URL line)
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
Resolves 5 architectural issues identified by auditing the full codebase.
Changes
fix(filler)— Replace fragile index-based PDF filling with name-based matchingannot.V = answers_list[i]— matched purely by position/indexannot.T(the PDF field label), decodes the PDF string, looks up value directly in the LLM answers dict (case-insensitive)None→ writes""(not the string"None"), list values → joined with"; ", unmatched fields → warning log instead of silent wrong fillfix(file_manipulator)— Eliminate race condition from shared mutable LLM stateself.llmwas mutated per-request — a data corruption race condition under concurrent requestsreturn Noneon missing PDF to a properFileNotFoundErrorfix(forms)— Remove duplicate get_template() DB callFileNotFoundErroris caught and surfaced as a 422 responsefix(main)— Register AppError exception handlerfix(llm)— Make Ollama model configurable viaOLLAMA_MODELenv var"mistral"— operators had to edit source code to change itos.getenv("OLLAMA_MODEL", "mistral")— backward compatible, defaults to"mistral"Relates to: #44