Skip to content

More accurate handling of LLM errors - #931

Merged
DavidMStraub merged 3 commits into
gramps-project:masterfrom
DavidMStraub:handle_llm_errors
Aug 12, 2026
Merged

More accurate handling of LLM errors#931
DavidMStraub merged 3 commits into
gramps-project:masterfrom
DavidMStraub:handle_llm_errors

Conversation

@DavidMStraub

Copy link
Copy Markdown
Member

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves how failures from the Pydantic AI agent / model provider are translated into HTTP errors, and adds regression tests to ensure specific exception types map to stable status codes.

Changes:

  • Add targeted exception handling in answer_with_agent() to distinguish provider HTTP errors (502) from transport/API failures (504).
  • Switch unexpected-error logging to logger.exception(...) for better traceback visibility.
  • Add a new pytest suite validating the status-code mapping for common agent/provider failure modes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
gramps_webapi/api/llm/__init__.py Refines exception-to-HTTP-status mapping for LLM agent execution errors.
tests/test_llm_errors.py Adds parametrized tests asserting the expected HTTP status codes for representative LLM/agent exceptions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread gramps_webapi/api/llm/__init__.py
@DavidMStraub
DavidMStraub requested a lite review from Copilot and removed request for Copilot August 12, 2026 07:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

gramps_webapi/api/llm/init.py:203

  • httpx.TransportError is broader than timeouts (it covers connect/protocol/other transport failures), but this handler maps all transport errors to HTTP 504 (gateway timeout). That will misclassify non-timeout upstream failures; consider catching httpx.TimeoutException separately for 504 and using a different status (e.g. 503) for other transport errors.
    except (ModelAPIError, httpx.TransportError) as e:
        # network failure or timeout talking to the provider
        logger.error("Model provider request failed: %r", e)
        abort_with_message(504, "The AI model did not respond. Please try again.")

@DavidMStraub
DavidMStraub merged commit 0f752a3 into gramps-project:master Aug 12, 2026
3 checks passed
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.

2 participants