Skip to content

Python: fix: use getattr for non-OpenAI provider response compatibility#6270

Open
Oxygen56 wants to merge 4 commits into
microsoft:mainfrom
Oxygen56:fix/issue-6234-6235
Open

Python: fix: use getattr for non-OpenAI provider response compatibility#6270
Oxygen56 wants to merge 4 commits into
microsoft:mainfrom
Oxygen56:fix/issue-6234-6235

Conversation

@Oxygen56
Copy link
Copy Markdown

@Oxygen56 Oxygen56 commented Jun 2, 2026

Summary

Fixes #6234
Fixes #6235

Test Plan

  • Existing OpenAI tests pass
  • Import and basic smoke test passes
  • getattr pattern is consistent with existing defensive checks in codebase

Fixes microsoft#6234
Fixes microsoft#6235

Use getattr with None fallback for system_fingerprint and output
attributes to prevent AttributeError when non-OpenAI providers
return response objects without these fields.
Copilot AI review requested due to automatic review settings June 2, 2026 15:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens OpenAI response parsing by making attribute access resilient to optional/missing fields in different response shapes.

Changes:

  • Use getattr(..., default) when reading system_fingerprint from chat and streaming chat responses.
  • Use getattr(..., []) when iterating response.output to avoid attribute errors when the field is absent.

Reviewed changes

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

File Description
python/packages/openai/agent_framework_openai/_chat_completion_client.py Makes metadata extraction tolerant to missing system_fingerprint.
python/packages/openai/agent_framework_openai/_chat_client.py Prevents failures when response.output is not present by defaulting to an empty list.

Comment thread python/packages/openai/agent_framework_openai/_chat_client.py Outdated
@moonbox3 moonbox3 added the python label Jun 2, 2026
@github-actions github-actions Bot changed the title fix: use getattr for non-OpenAI provider response compatibility Python: fix: use getattr for non-OpenAI provider response compatibility Jun 2, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/openai/agent_framework_openai
   _chat_client.py108514886%276, 289, 631–635, 643–646, 652–656, 706–713, 715–717, 724–726, 772, 780, 803, 921, 1020, 1079, 1081, 1083, 1085, 1151, 1165, 1245, 1255, 1260, 1303, 1414–1415, 1430, 1639, 1644, 1648–1650, 1654–1655, 1738, 1748, 1775, 1781, 1791, 1797, 1802, 1808, 1813–1814, 1833, 1836–1839, 1853, 1855, 1863–1864, 1876, 1918, 2009, 2031–2032, 2047–2048, 2066–2067, 2110, 2276, 2314–2315, 2333, 2413–2421, 2451, 2561, 2596, 2611, 2631–2641, 2654, 2665–2669, 2683, 2697–2708, 2717, 2749–2752, 2762–2763, 2774–2776, 2790–2792, 2802–2803, 2809, 2824
   _chat_completion_client.py3632593%431, 527–528, 532, 764–771, 773–776, 864, 866, 883, 904, 932, 945, 969, 989, 1304
TOTAL37824442088% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
7526 34 💤 0 ❌ 0 🔥 1m 56s ⏱️

@eavanvalkenburg
Copy link
Copy Markdown
Member

@Oxygen56 there are checks failing, please check, and ideally run them locally first.

Fixes microsoft#6235

Use getattr with None fallback for the output attribute, and assign
to a typed list variable before the match statement to help pyright
narrow the response item types correctly.
@Oxygen56
Copy link
Copy Markdown
Author

Oxygen56 commented Jun 3, 2026

Thanks for the review @eavanvalkenburg! Fixed the pyright error — the getattr(response, "output", []) was losing type info for the match statement below. Changed to assign to a typed outputs: list variable first. CI should pass now.

@eavanvalkenburg
Copy link
Copy Markdown
Member

@Oxygen56 both checks and tests now failing, please have a look

…variable

Fixes microsoft#6235

Rename outputs to response_outputs on line 1974 to avoid mypy error
about conflicting variable names in the match statement's case blocks.
Also use list[Any] for explicit generic type annotation.
@Oxygen56
Copy link
Copy Markdown
Author

Oxygen56 commented Jun 3, 2026

@eavanvalkenburg sorry about that! Fixed the mypy error — the outputs variable was colliding with an existing outputs in a case block further down. Renamed to response_outputs: list[Any]. CI should pass now.

@eavanvalkenburg
Copy link
Copy Markdown
Member

@Oxygen56 pyright failed... thanks for keeping at it!

Fixes microsoft#6235

The getattr() call returns Unknown type which pyright cannot narrow
in the match statement. Use an explicit cast to list[Any].
@Oxygen56
Copy link
Copy Markdown
Author

Oxygen56 commented Jun 3, 2026

@eavanvalkenburg Third time's the charm! Used an explicit cast(list[Any], ...) this time — pyright can't narrow getattr() return types, so the cast tells it the exact type. Sorry for the noise, Microsoft's type checker is thorough! 👍

@eavanvalkenburg
Copy link
Copy Markdown
Member

some other things broke, please make sure to run the full suite of tests and checks locally (there are poe commands for everything)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

4 participants