Skip to content

Python: update FoundryAgent for hosted agent sessions#5447

Open
eavanvalkenburg wants to merge 3 commits intomicrosoft:mainfrom
eavanvalkenburg:fix_foundry_agents_for_new_hosted_agents
Open

Python: update FoundryAgent for hosted agent sessions#5447
eavanvalkenburg wants to merge 3 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

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.

Co-authored-by: Copilot <copilot@github.com>
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/foundry/agent_framework_foundry
   _agent.py2265874%118, 121, 228–229, 233–235, 240–243, 334, 378, 384–386, 404–405, 417–418, 430–432, 434–435, 437–443, 445–446, 448, 450, 456–458, 461–470, 474–475, 662–663, 666, 692, 702, 718, 783
   _chat_client.py1541987%84, 86–88, 92–93, 97, 191, 226, 319, 380, 382, 480, 484–485, 487–490
packages/foundry_hosting/agent_framework_foundry_hosting
   _responses.py42713468%130–131, 140–141, 145, 150, 164, 189, 217–219, 239–241, 243–245, 247–249, 253–256, 259–264, 271, 276, 279, 286–287, 289–290, 292, 294, 296–299, 301–303, 306, 310, 312–319, 322–323, 325–327, 335–340, 412–413, 541, 805–807, 809, 827–856, 858, 876, 879, 914–918, 922–928, 935–939, 945–951, 958, 964, 967
TOTAL29142348088% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5803 30 💤 0 ❌ 0 🔥 1m 32s ⏱️

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

Co-authored-by: Copilot <copilot@github.com>
Comment thread python/packages/foundry/agent_framework_foundry/_agent.py Outdated
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>
openai_client_kwargs["default_headers"] = dict(default_headers)
if allow_preview:
openai_client_kwargs["agent_name"] = self.agent_name
async_client = self.project_client.get_openai_client(**openai_client_kwargs)
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.

See the comments in this PR: #5433

raise ValueError("isolation_key is required. Pass it explicitly or set default_options['isolation_key'].")
return resolved_isolation_key

async def _create_service_session_id(
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.

Do we need to do this? I think if the request contains a new session ID, the service will automatically create one for the client.

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.

Is this a left over file from testing the project?

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.

Let's not change this for now.

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.

Let's not modify this sample for now. I will take a pass on the samples once the once in the Foundry get more stable.

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.

4 participants