Python: Foundry hosted agent responses emit failed events#6502
Python: Foundry hosted agent responses emit failed events#6502TaoChenOSU wants to merge 1 commit into
Conversation
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
This PR updates the Foundry hosted responses handler so that exceptions raised during response creation (both streaming SSE and non-streaming JSON) are surfaced as terminal response.failed events carrying the exception message, rather than bubbling up as generic HTTP 5xx errors.
Changes:
- Wrap agent/workflow response generation in exception handling that emits
response.failed(and drains any active streaming output item first to keep SSE well-formed). - Add a shared
_emit_failure(...)helper to standardize failure emission behavior. - Update and extend tests to assert
HTTP 200withstatus="failed"and meaningfulerror.messagefor failure scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py |
Emits terminal response.failed on exceptions (streaming + non-streaming), draining any active streaming output item first via a new helper. |
python/packages/foundry_hosting/tests/test_responses.py |
Updates existing assertions away from 5xx and adds new coverage ensuring response.failed emission + error message propagation. |
There was a problem hiding this comment.
Automated Code Review
Reviewers: 3 | Confidence: 81%
✓ Security Reliability
The PR correctly converts raw exceptions into well-formed
response.failedprotocol events, preserving existing security invariants (path traversal rejection, no filesystem artifacts). The implementation is sound: tracker drainage is defensively wrapped, the stream lifecycle always terminates properly, and exception types are correctly scoped toException(notBaseException). The one concern is thatstr(ex)unconditionally forwards raw exception messages to the client, which could disclose internal infrastructure details (file paths, connection strings) from unexpected library failures, though this is a deliberate design choice with explicit test coverage.
✓ Test Coverage
The PR adds good test coverage for the primary error-surfacing paths (non-streaming, streaming with partial output, workflow agent failures). However, the
_emit_failurehelper at line 734 has two explicitly coded defensive branches—the empty-message fallback (type(ex).__name__) and the try/except aroundtracker.close()—that have no corresponding tests. Additionally, the streaming workflow agent failure path in_handle_inner_workflowis exercised only for non-streaming mode in the new test class.
✓ Design Approach
I did not find a design-approach issue that I could support with repo-backed file:line evidence. The new handlers consistently convert agent/workflow exceptions into terminal
response.failedevents, which matches the new tests in this PR and is also consistent with existing response-failure patterns elsewhere in the repo.
Suggestions
- Add a test for the empty-message fallback in
_emit_failure(e.g., raise a bareRuntimeError()with no message and assert the error message equals the exception type name). This exercises thestr(ex) or type(ex).__name__branch at _responses.py:734. - Add a test that verifies when
tracker.close()itself raises during failure handling, the original exception's message is still what the client receives (exercises the try/except at _responses.py:730-733).
Automated review by TaoChenOSU's agents
Motivation and Context
Previously, if the agent/workflow fails in Foundry Hosted Agent, exceptions will be raised and server error will be returned to users without any detail. Users will have to go to the log stream to figure out what the error is. This is not a great experience.
Description
Instead of the raising exceptions, we now catch the exceptions and emit failed events.
Before:

After:

Contribution Checklist