Skip to content

Python: update FoundryAgent for hosted agent sessions#5447

Merged
eavanvalkenburg merged 9 commits intomicrosoft:mainfrom
eavanvalkenburg:fix_foundry_agents_for_new_hosted_agents
Apr 24, 2026
Merged

Python: update FoundryAgent for hosted agent sessions#5447
eavanvalkenburg merged 9 commits intomicrosoft:mainfrom
eavanvalkenburg:fix_foundry_agents_for_new_hosted_agents

Conversation

@eavanvalkenburg
Copy link
Copy Markdown
Member

@eavanvalkenburg eavanvalkenburg commented Apr 23, 2026

Motivation and Context

Foundry hosted agents now rely on preview session APIs, and the current Python Foundry agent and sample flow did not handle lazy service session creation or the new hosted request payload shape. This change aligns the Foundry agent, foundry_hosting server, and hosted-agent samples with the new hosted-agent flow.

Description

  • add hosted-agent session support to FoundryAgent and RawFoundryAgent, including FoundryAgentOptions, lazy service-session creation and deletion, and agent-session mapping through AgentSession.service_session_id and conversation_id
  • pass hosted-agent request data through the Foundry chat client while preserving caller extra_body, and document the allow_preview=True requirement for HostedAgent preview session APIs
  • refresh the hosted-agent samples and fix foundry_hosting non-streaming response handling and related tests

Closes: #5416

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings April 23, 2026 14:35
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@moonbox3 moonbox3 added documentation Improvements or additions to documentation python labels Apr 23, 2026
@moonbox3
Copy link
Copy Markdown
Contributor

moonbox3 commented Apr 23, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _telemetry.py52590%63, 78–80, 126
packages/foundry/agent_framework_foundry
   _agent.py2265874%118, 121, 228–229, 233–235, 240–243, 333, 377, 383–385, 403–404, 416–417, 429–431, 433–434, 436–442, 444–445, 447, 449, 455–457, 460–469, 473–474, 661–662, 665, 691, 701, 717, 782
   _chat_client.py1551987%84, 86–88, 92–93, 97, 191, 226, 319, 380, 382, 480, 484–485, 487–490
packages/foundry_hosting/agent_framework_foundry_hosting
   _responses.py50611477%150–151, 160–161, 165, 170, 184, 208, 234–236, 255–257, 259–261, 263–265, 269–272, 275–280, 287, 292, 295, 302–303, 305–306, 308, 310, 312–315, 317–319, 323, 326–333, 336–337, 339–340, 348–353, 425–426, 1071–1073, 1075, 1097–1098, 1100–1101, 1103–1104, 1106–1107, 1118–1122, 1124, 1142, 1145, 1180–1184, 1188–1194, 1201–1205, 1211–1217, 1224, 1230, 1233
TOTAL29331345688% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5899 30 💤 0 ❌ 0 🔥 1m 38s ⏱️

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 3 | Confidence: 92%

✗ Correctness

The _handle_inner_agent method in _responses.py has an inverted isinstance(self._agent, RawAgent) check: it warns and skips options for RawAgent (which supports options in .run() — see _agents.py:896) and passes options to non-RawAgent agents (whose SupportsAgentRun.run() at _agents.py:271-278 has no options parameter). This inverts the correct old behavior. The corresponding tests were updated to match the inverted logic (using raw_agent=False), masking the bug. The rest of the changes (FoundryAgentOptions, service session management, extra_body merging, allow_preview plumbing) look correct.

✓ Test Coverage

The PR adds substantial new functionality (FoundryAgentOptions, service session management, extra_body merging, conversation_id suppression) with generally good test coverage for the happy paths. However, there are notable test gaps: the streaming _parse_chunk_from_openai override has zero test coverage despite being a mirror of the tested _parse_response_from_openai; the allow_preview=True branch that preserves tools/tool_choice in _prepare_options is untested; and get_agent_version() error paths (TypeError raises) lack coverage. The _responses.py refactoring inverts the RawAgent/non-RawAgent options-forwarding logic, and the test was updated to match the new non-RawAgent path, but no test verifies the new RawAgent warning behavior.

✗ Design Approach

I found two design-level problems in the new hosted-session path. The first is a real identity mix-up: the code now allows the local wrapper name to diverge from the remote Foundry agent_name, but the new session-management methods call the service with self.name, so a custom display name will target the wrong hosted agent. The second is an abstraction mismatch around preview enablement: the docs say a caller can pass an already preview-configured project_client, but the implementation gates hosted-session behavior off the wrapper's allow_preview flag instead of the supplied client capability, so that path is disabled unless callers redundantly set both.

Suggestions

  • Add a streaming-path test for _parse_chunk_from_openai analogous to test_raw_foundry_agent_chat_client_parse_response_suppresses_conversation_id_for_agent_sessions — the conversation_id suppression logic is identical but has zero test coverage.
  • Model preview enablement as a single source of truth — either require the wrapper flag consistently and document it, or derive capability from the supplied project_client — so calers don't need to configure both.

Automated review by eavanvalkenburg's agents

Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry/agent_framework_foundry/_agent.py Outdated
Comment thread python/packages/foundry/agent_framework_foundry/_agent.py Outdated
Comment thread python/packages/foundry/agent_framework_foundry/_agent.py
Comment thread python/samples/04-hosting/foundry-hosted-agents/responses/01_basic/.gitignore Outdated
eavanvalkenburg and others added 6 commits April 24, 2026 09:41
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Remove the public hosted-agent service session CRUD helpers from FoundryAgent and drop the related feature-stage inventory entry.

Update the hosted-agent sample to create and delete service sessions directly through the preview AIProjectClient APIs, and tighten a few test harnesses surfaced by full workspace validation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <copilot@github.com>
@eavanvalkenburg eavanvalkenburg force-pushed the fix_foundry_agents_for_new_hosted_agents branch from 34813e0 to 0363a42 Compare April 24, 2026 07:42
eavanvalkenburg and others added 2 commits April 24, 2026 09:53
Co-authored-by: Copilot <copilot@github.com>
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Apr 24, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 24, 2026
Co-authored-by: Copilot <copilot@github.com>
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Apr 24, 2026
Merged via the queue into microsoft:main with commit 62e02da Apr 24, 2026
33 checks passed
moonbox3 added a commit to moonbox3/agent-framework that referenced this pull request Apr 24, 2026
Previous commit incorrectly renamed the [1.1.1] header to [1.2.0], which
wiped the historical 1.1.1 entries and wrongly attributed them to 1.2.0.
This restores [1.1.1] to its origin/main content and adds a new [1.2.0]
section above containing only the commits in python-1.1.1..HEAD:

- microsoft#4238 functional workflow API
- microsoft#5142 GitHub Copilot OpenTelemetry
- microsoft#2403 A2A bridge support
- microsoft#5070 oauth_consent_request events in Foundry clients
- microsoft#5447 FoundryAgent hosted agent sessions
- microsoft#5459 hosting server dependency upgrade + types
- microsoft#5389 AG-UI reasoning/multimodal parsing fix
- microsoft#5440 stop [TOOLBOXES] warning spam
- microsoft#5455 user agent prefix fix

Also corrects the [1.2.0] compare base to python-1.1.1 (not 1.1.0) and
adds the missing [1.1.1] reference link.
moonbox3 added a commit that referenced this pull request Apr 24, 2026
* Bump Python package versions for 1.2.0 release

Released tier bumps 1.1.1 -> 1.2.0 (core, openai, foundry, root) to
reflect additive public APIs landed since 1.1.0: functional workflow API
(#4238) and FunctionTool SKIP_PARSING sentinel (#5424). All beta packages
stamped 1.0.0b260424, alpha packages 1.0.0a260424. All 26 non-core
agent-framework-core floors raised to >=1.2.0,<2. CHANGELOG consolidates
the never-tagged 1.1.1 entries with the post-merge additions into [1.2.0].

* Update CHANGELOG footer links for 1.2.0

Advance [Unreleased] comparison base from python-1.1.0 to python-1.2.0
and add a [1.2.0] reference link comparing python-1.1.0...python-1.2.0
so the heading links resolve correctly.

* Fix CHANGELOG: restore [1.1.1] section and add proper [1.2.0]

Previous commit incorrectly renamed the [1.1.1] header to [1.2.0], which
wiped the historical 1.1.1 entries and wrongly attributed them to 1.2.0.
This restores [1.1.1] to its origin/main content and adds a new [1.2.0]
section above containing only the commits in python-1.1.1..HEAD:

- #4238 functional workflow API
- #5142 GitHub Copilot OpenTelemetry
- #2403 A2A bridge support
- #5070 oauth_consent_request events in Foundry clients
- #5447 FoundryAgent hosted agent sessions
- #5459 hosting server dependency upgrade + types
- #5389 AG-UI reasoning/multimodal parsing fix
- #5440 stop [TOOLBOXES] warning spam
- #5455 user agent prefix fix

Also corrects the [1.2.0] compare base to python-1.1.1 (not 1.1.0) and
adds the missing [1.1.1] reference link.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: FoundryChatClient(default_headers=...) and FoundryAgent(default_headers=...) silently drop custom headers

5 participants