Skip to content

Python: Convert nested SDK models in hosted tool sanitization - #9789

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
Dhruv-Sh-git:fix/issue-9784
Nov 3, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
Dhruv-Sh-git:fix/issue-9784

Conversation

@Dhruv-Sh-git

@Dhruv-Sh-git Dhruv Shaswat (Dhruv-Sh-git) commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9784.

Summary

Passing tools returned by FoundryChatClient.get_bing_custom_search_tool() or FoundryChatClient.get_code_interpreter_tool() directly to an agent or _prepare_tools_for_openai() could fail during JSON serialization with a TypeError.
The issue was that _sanitize_foundry_response_tool() only made a shallow copy of the tool mapping. Nested Azure SDK models were left unchanged, so they couldn't be serialized as JSON.

Changes

  • Updated _tools.py to recursively convert nested SDK models into plain Python data using their .as_dict() representation, including values inside mappings and sequences.
  • Added regression tests in test_foundry_chat_client.py for both Bing custom search and code interpreter tools, checking that they produce valid JSON payloads through _prepare_tools_for_openai().

Testing

Validated locally with Ruff and pytest: 464 tests passed in packages/foundry.

Copilot AI balanced review requested due to automatic review settings October 28, 2026 09:32

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@eavanvalkenburg

Copy link
Copy Markdown
Member

/review

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): f2ebec8206d3
Model: gpt-5.6-sol

Overview

The change fixes the reported serialization failure by recursively converting nested Azure SDK models, and the new tests exercise both affected factories through the real preparation path and json.dumps(). Existing validation, field stripping, and explicit container preservation remain intact. However, checking for as_dict before handling mappings introduces a compatibility regression for otherwise valid mapping-based hosted tools.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/foundry/agent_framework_foundry/_tools.py

@eavanvalkenburg

Copy link
Copy Markdown
Member

Thanks for the fix, Dhruv Shaswat (@Dhruv-Sh-git). The Azure SDK tool model already provides the canonical recursive conversion through as_dict(), and the issue confirms that path produces the correct JSON-safe payload. Rather than maintaining a generic recursive hasattr(as_dict) converter in Agent Framework, please use the SDK’s canonical conversion for recognized Azure SDK tool models at the hosted-tool boundary, then apply the existing sanitization to the resulting plain mapping. Keep arbitrary caller-supplied Mapping objects on normal mapping semantics, and retain regressions for Bing Custom Search, code interpreter, and mappings with unrelated or incompatible as_dict methods.

Merged via the queue into microsoft:main with commit 65b2ee6 Nov 3, 2026
50 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — b49e2922 Deployed Oct 30, 2026 by Dhruv-Sh-git via add_label #26756
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.

Python: [Bug]: FoundryChatClient: hosted tools from get_bing_custom_search_tool() fail JSON serialization (nested SDK models not converted)

3 participants