fix(pydantic-ai): Add error handling to agent execution#92
Conversation
Wrap the agent.run() call in a try-except block to properly handle exceptions that occur during agent execution. This allows the test to continue running and capture error information instead of crashing. Co-Authored-By: Claude <noreply@anthropic.com>
🟡 AI SDK Integration Test ResultsStatus: 168 tests failing (no regressions) Summary
Test MatrixAgent Tests
Embedding Tests
LLM Tests
Legend: ✅ Pass | ❌ Fail | ✅🔧 Fixed | ❌📉 Regressed | ✅🆕 New (pass) | ❌🆕 New (fail) | 🗑️ Removed | str=streaming blk=blocking a=async s=sync Generated by AI SDK Integration Tests |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Swallowed exceptions prevent sentry_sdk.capture_exception from firing
- Added 'raise' statement after printing exception to allow it to propagate to base template's error handler.
Or push these changes by commenting:
@cursor push 4a222eb538
Preview (4a222eb538)
diff --git a/src/runner/templates/agents/python/pydantic-ai/template.njk b/src/runner/templates/agents/python/pydantic-ai/template.njk
--- a/src/runner/templates/agents/python/pydantic-ai/template.njk
+++ b/src/runner/templates/agents/python/pydantic-ai/template.njk
@@ -73,6 +73,7 @@
print(f"Turn {{ loop.index }} Response: {result.output}")
except Exception as e:
print(f"Turn {{ loop.index }} Error: {type(e).__name__}: {e}")
+ raise
{% if not loop.last %}
print() # Blank line between turns
{% endif %}| # Print response | ||
| print(f"Turn {{ loop.index }} Response: {result.output}") | ||
| except Exception as e: | ||
| print(f"Turn {{ loop.index }} Error: {type(e).__name__}: {e}") |
There was a problem hiding this comment.
Swallowed exceptions prevent sentry_sdk.capture_exception from firing
High Severity
The new except Exception as e block catches and prints the error but never re-raises it. When the causeAPIError flag is set, base.python.njk wraps the main() call in its own try-except that calls sentry_sdk.capture_exception(e) — but the exception is now silently swallowed inside main(), so it never propagates. This means Sentry never captures the exception, breaking the error-handling test scenarios that depend on it.
There was a problem hiding this comment.
I don't think this is entirely accurate as I verified during testing that an error span is being returned from the Python SDK (and not being swallowed as Cursor is saying here) as a result of the generic exception thrown, and is being asserted correctly within the testing framework (example below from a test run):
{
trace_id: '25a8c19c306a42a2b1bd22b34daa3e84',
span_id: '85d009fd8743fdb6',
parent_span_id: '905cfffa695c1f38',
same_process_as_parent: true,
op: 'gen_ai.execute_tool',
description: 'execute_tool read_file',
start_timestamp: '2026-03-06T16:13:37.239475Z',
timestamp: '2026-03-06T16:13:37.240453Z',
origin: 'auto.ai.pydantic_ai',
status: 'internal_error',
tags: { status: 'internal_error' },
data: {
'thread.id': '8372809984',
'thread.name': 'MainThread',
'gen_ai.operation.name': 'execute_tool',
'gen_ai.tool.type': 'function',
'gen_ai.tool.name': 'read_file',
'gen_ai.agent.name': 'file_assistant',
'gen_ai.tool.input': '{"path": "/nonexistent/file.txt"}'
}
}
constantinius
left a comment
There was a problem hiding this comment.
Looks good, thank you!



Fix exception handling in pydantic-ai agent template
The pydantic-ai template's agent execution was not handling exceptions
that could occur during
agent.run()calls. This caused test executionto crash instead of gracefully handling errors.
Wrapping the agent execution in a try-except block allows the test to
continue running and properly capture error information when exceptions
occur, making the tests more robust and providing better visibility
into failures.
This fix enables proper validation of error handling behavior in the
pydantic-ai integration tests.
The tests failing within this suite are due to missing fixes in PRs getsentry/sentry-python#5596 and #89