Skip to content

🎯 Fix ReAct agent bypassing document_qa for legal questions - #7

Merged
GiorgosPanagopoulos merged 2 commits into
mainfrom
fix/agent-legal-routing
Aug 10, 2026
Merged

🎯 Fix ReAct agent bypassing document_qa for legal questions#7
GiorgosPanagopoulos merged 2 commits into
mainfrom
fix/agent-legal-routing

Conversation

@GiorgosPanagopoulos

Copy link
Copy Markdown
Owner

Bug

The chat agent was answering Greek procurement-law questions from model knowledge instead of routing to document_qa, bypassing the N.4412/2016 RAG layer entirely — so answers came back with no article citations and no sources.

Repro: "Ποια είναι τα όρια απευθείας ανάθεσης;" through /chat returned intermediate_steps=[] and tool_used="unknown". The same question through /doc_qa correctly cited Άρθρο 118. Retrieval was healthy — only tool selection was wrong.

Fix

Added a new first rule to the TOOL SELECTION RULES block in backend/prompts/chat/v1.txt, forcing document_qa for any message about N.4412/2016, legal thresholds, award/tender procedures, guarantees, deadlines, or specific articles.

The rule had to be worded for bare topic phrases, not just questions: the suggestion chips in the UI are noun-phrase labels (e.g. "Συνοπτικός διαγωνισμός"), not interrogative sentences. A rule written as "for any question about X" didn't match them — the agent's own reasoning trace showed it explicitly deciding no tool was needed because the user hadn't asked a specific question. The rule now covers bare topic/keyword input too.

Verification

Logged in, sent all six Greek suggestion chips to /chat, and checked the resulting tool call via:

docker compose exec mongo mongosh procureai --quiet --eval \
  'db.conversations.find({},{query:1,"trace.tool":1,_id:0}).sort({_id:-1}).limit(6).toArray()'
Chip Tool resolved
Όρια απευθείας ανάθεσης document_qa
Συνοπτικός διαγωνισμός document_qa
Εγγύηση καλής εκτέλεσης document_qa
Σύγκριση προσφορών bid_comparison
Εύρεση προμηθευτών IT supplier_lookup
Δημιουργία αναφοράς report_generation

All three legal chips now route to document_qa; the other three still route correctly. Repeated the previously-flaky chip multiple times to confirm the fix is stable rather than a lucky sample. Full backend test suite passes (160 passed).

Also

Added a README note: docker compose restart backend does not pick up backend code or prompt changes since there is no source bind-mount — it silently reuses the old image. Use docker compose build backend && docker compose up -d backend instead. This cost two silent no-op verification cycles while debugging the issue above.

The chat agent's TOOL SELECTION RULES only forced document_qa for
prices/contracts, so questions about N.4412/2016 thresholds, award
procedures, and articles were answered from model knowledge instead
of the RAG-indexed law text — no article citations, no sources, and
tool_used came back as "unknown" with an empty trace.

Add legal topics as the first tool-selection rule, and make it apply
even to bare topic/keyword input (e.g. "Συνοπτικός διαγωνισμός")
rather than only full questions, since the model was otherwise
treating unphrased topics as not covered by the rule.
docker compose restart backend reuses the existing image since the
backend service has no source bind-mount, so edits to backend code
or prompts silently no-op until the image is rebuilt. Cost two full
verification cycles while fixing the legal-routing bug.
@GiorgosPanagopoulos
GiorgosPanagopoulos merged commit 714478b into main Aug 10, 2026
2 checks passed
@GiorgosPanagopoulos
GiorgosPanagopoulos deleted the fix/agent-legal-routing branch August 10, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant