Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tweak exception messages; error_log->help_message
  • Loading branch information
sidnarayanan committed Sep 24, 2025
commit 83e5ff4509555f0f250eceea2b9ebea6f20d4dca
10 changes: 5 additions & 5 deletions src/paperqa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def fraction_replacer(match: re.Match) -> str:

class LLMContextError(ValueError):
retryable: ClassVar[bool]
error_log: ClassVar[str]
help_message: ClassVar[str] # Eventually passed to logger.exception

def __init__(self, message: str, llm_results: list[LLMResult]) -> None:
super().__init__(message)
Expand All @@ -138,7 +138,7 @@ class LLMBadContextJSONError(LLMContextError):

retryable = True
error_log = (
"Abandoning this context."
"Abandoning this context creation."
" Your model may not be capable of supporting JSON output"
" or our parsing technique could use some work. Try"
" a different model or specify `Settings(prompts={'use_json': False})`."
Expand Down Expand Up @@ -270,7 +270,7 @@ async def _map_fxn_summary( # noqa: PLR0912
used_text_only_fallback = True
except litellm.Timeout as exc:
raise LLMContextTimeoutError(
f"LLM call to create a context timed out on text named {text.name!r}: {exc}",
f"LLM call to create a context timed out on text named {text.name!r}.",
llm_results=llm_results,
) from exc

Expand Down Expand Up @@ -362,11 +362,11 @@ async def map_fxn_summary(**kwargs) -> tuple[Context | None, list[LLMResult]]:
except LLMContextError as exc:
if not exc.retryable:
logger.exception(
"Non-retryable failure creating a context. %s", exc.error_log
"Non-retryable failure creating a context. %s", exc.help_message
)
return None, exc.llm_results
try:
return await _map_fxn_summary(**kwargs, _prior_attempt=exc)
except LLMContextError as exc2:
logger.exception("Failed twice to create a context. %s", exc2.error_log)
logger.exception("Failed twice to create a context. %s", exc2.help_message)
return None, exc2.llm_results
29 changes: 16 additions & 13 deletions tests/test_paperqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ def test_multiple_authors() -> None:


def test_multiple_citations() -> None:
text = "As discussed by several authors (Smith et al. 1999; Johnson 2001; Lee et al. 2003)."
text = (
"As discussed by several authors (Smith et al. 1999; Johnson 2001; Lee et al."
" 2003)."
)
assert strip_citations(text) == "As discussed by several authors ."


Expand Down Expand Up @@ -623,6 +626,7 @@ async def test_query(docs_fixture) -> None:

@pytest.mark.asyncio
async def test_custom_context_str_fn(docs_fixture) -> None:

async def custom_context_str_fn( # noqa: RUF029
settings: Settings, # noqa: ARG001
contexts: list[Context], # noqa: ARG001
Expand All @@ -648,6 +652,7 @@ async def custom_context_str_fn( # noqa: RUF029

@pytest.mark.asyncio
async def test_aquery_groups_contexts_by_question(docs_fixture) -> None:

session = PQASession(question="What is the relationship between chemistry and AI?")

doc = Doc(docname="test_doc", citation="Test Doc, 2025", dockey="key1")
Expand Down Expand Up @@ -734,14 +739,12 @@ async def test_query_with_iteration(docs_fixture) -> None:
prior_session = PQASession(question=question, answer=prior_answer)
await docs_fixture.aquery(prior_session, llm_model=llm, settings=settings)
assert prior_answer in cast(
"str",
my_results[-1].prompt[1].content, # type: ignore[union-attr, index]
"str", my_results[-1].prompt[1].content # type: ignore[union-attr, index]
), "prior answer not in prompt"
# run without a prior session to check that the flow works correctly
await docs_fixture.aquery(question, llm_model=llm, settings=settings)
assert settings.prompts.answer_iteration_prompt[:10] not in cast( # type: ignore[index]
"str",
my_results[-1].prompt[1].content, # type: ignore[union-attr, index]
"str", my_results[-1].prompt[1].content # type: ignore[union-attr, index]
), "prior answer prompt should not be inserted"


Expand Down Expand Up @@ -965,9 +968,7 @@ class StubLLMModel(LLMModel):
name: str = "custom/myllm"

async def acompletion(
self,
messages: list[Message],
**kwargs, # noqa: ARG002
self, messages: list[Message], **kwargs # noqa: ARG002
) -> list[LLMResult]:
return [
LLMResult(
Expand All @@ -981,9 +982,7 @@ async def acompletion(

@rate_limited
async def acompletion_iter(
self,
messages: list[Message],
**kwargs, # noqa: ARG002
self, messages: list[Message], **kwargs # noqa: ARG002
) -> AsyncIterable[LLMResult]:
yield LLMResult(
model=self.name,
Expand Down Expand Up @@ -1715,7 +1714,10 @@ async def test_custom_prompts(stub_data_dir: Path) -> None:

@pytest.mark.asyncio
async def test_pre_prompt(stub_data_dir: Path) -> None:
pre = "What is water's boiling point in Fahrenheit? Please respond with a complete sentence."
pre = (
"What is water's boiling point in Fahrenheit? Please respond with a complete"
" sentence."
)

settings = Settings.from_name("fast")
settings.prompts.pre = pre
Expand Down Expand Up @@ -2443,7 +2445,8 @@ def test_llm_parse_json_with_escaped_characters(self, input_text, expected_outpu
def test_llm_subquotes_and_newlines(self, input_text: str) -> None:
output = {
"summary": (
'An excerpt with "quoted stuff" or "maybe more." More stuff (with parenthesis).'
'An excerpt with "quoted stuff" or "maybe more." More stuff (with'
" parenthesis)."
),
"relevance_score": 8,
}
Expand Down
Loading