Skip to content

Python: Fix: readable TypeError for union response_type mismatch (no __name__ crash on 3.10/3.11) - #9335

Open
PratikDhanave (PratikDhanave) wants to merge 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix/request-info-response-type-display
Open

PratikDhanave (PratikDhanave) wants to merge 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix/request-info-response-type-display

Conversation

@PratikDhanave

Copy link
Copy Markdown

Motivation & Context

InProcRunnerContext.send_request_info_response validates the response against the request's response_type and, on mismatch, raises TypeError with a message built from event.response_type.__name__.

response_type is not restricted to plain classes — is_instance_of(data, target_type: type | UnionType | Any) explicitly accepts unions, and callers pass them (e.g. response_type=int | str). types.UnionType has no __name__ on Python 3.10/3.11 (it was only added in 3.12, gh-105499). Since the package declares requires-python = ">=3.10", on those interpreters the error path itself raises AttributeError — masking the intended TypeError with a confusing, unrelated exception. On 3.12+ it doesn't crash but yields the unhelpful "expected Union".

Description & Review Guide

Compute the display name defensively: use __name__ for real classes, and str(...) for anything else (unions, generics):

expected = (
    event.response_type.__name__
    if isinstance(event.response_type, type)
    else str(event.response_type)
)
  • Impact: mismatch errors now name the actual expected type on every supported Python — int | str, list[int], as well as plain int. No API/signature change.
  • Tests: added a regression test with response_type=int | str and a mismatched (float) response, asserting the message reads expected int | str, got float. On 3.10/3.11 the old code raised AttributeError (no TypeError at all); on 3.12+ it produced expected Union. Fails before, passes after. Full test_request_info_event_rehydrate.py suite green (11 passed).

Related Issue

N/A - trivial change: a localized hardening of one error-message expression, covered by a new unit test.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: This change was developed with Claude Code (an AI agent) — it located the bug, wrote the fix and the regression test, and ran verification (pytest). PratikDhanave (@PratikDhanave) to review and tick the AI-assisted attestation above before merge.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible

… crash)

send_request_info_response built the mismatch message with
event.response_type.__name__. response_type may legitimately be a union
(is_instance_of accepts type | UnionType | Any, and callers pass e.g.
int | str). On Python 3.10/3.11 types.UnionType has no __name__, so the
error path itself raised AttributeError instead of the intended TypeError;
on 3.12+ it only yields the unhelpful "Union".

Use response_type.__name__ for plain classes and str(response_type) for
unions/generics, so the message names the actual type ("int | str",
"list[int]") on every supported Python version.

Add a regression test (response_type=int | str, mismatched response)
asserting the message names the union.
Copilot AI balanced review requested due to automatic review settings October 13, 2026 12:49
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Oct 13, 2026
@github-actions github-actions Bot changed the title Fix: readable TypeError for union response_type mismatch (no __name__ crash on 3.10/3.11) Python: Fix: readable TypeError for union response_type mismatch (no __name__ crash on 3.10/3.11) Oct 13, 2026

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.

🟡 Changes recommended

The new union argument conflicts with the typed request-info API and will fail test type-checking.

1 open finding
What changed in this PR

Improves workflow response mismatch errors for union and generic response types.

Changes:

  • Adds defensive expected-type formatting.
  • Adds a union mismatch regression test.
File Description
_runner_context.py Formats non-class response types readably.
test_request_info_event_rehydrate.py Tests union mismatch errors.

🧠 Review effort: Balanced


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

request_id="union-req",
source_executor_id="gateway",
request_data=MockRequest(),
response_type=int | str,

This branch was successfully deployed

1 active deployment
github-app-auth — 603c3b27 Deployed Oct 13, 2026 by PratikDhanave via team_check #6697
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants