Fixing gen_answer failover leaving raw_answer blank#1077
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an issue where the EmptyDocsError failover in gen_answer was leaving raw_answer blank in the PQASession. The changes ensure that when no papers are available, the answer generation process still populates these fields through the normal flow while maintaining specialized error messages in tool responses.
Key changes:
- Removes early return from
gen_answertool when no docs are present - Updates answer generation logic to handle empty contexts with specific error messages
- Refactors test to properly verify tool response content
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/paperqa/agents/tools.py | Removes early EmptyDocsError check to allow normal flow processing |
| src/paperqa/docs.py | Updates empty context detection and error message formatting |
| src/paperqa/prompts.py | Adds constant for measuring empty context length |
| tests/test_agents.py | Refactors test to spy on tool responses instead of mocking dependencies |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
0eedbd8 to
dae79ff
Compare
| pre_str=pre_str, | ||
| ) | ||
|
|
||
| if len(context_str.strip()) < 10: # noqa: PLR2004 |
There was a problem hiding this comment.
Given empty context_str is 11-chars, we couldn't hit this case actually
| if not state.docs.docs: | ||
| raise EmptyDocsError("Not generating an answer due to having no papers.") |
There was a problem hiding this comment.
Note that state.session.raw_answer was left untouched here. This is fine in normal gen_answer use, but is not okay in the truncation's gen_answer failover. So this is a bug this PR fixes
#1073 was suboptimal in that if the
EmptyDocsErrorroute was taken, thePQASession'sraw_answer/answer_reasoningwould be left unpopulated withoutpatching in-place. This PR:raw_answer/answer_reasoningin a normal flow