Python: Fix FoundryAgent inheriting OPENAI_CHAT_MODEL for agent-reference requests - #7283
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 4 commits intoJul 30, 2026
Conversation
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 23, 2026 10:19 — with
GitHub Actions
Inactive
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 23, 2026 10:19 — with
GitHub Actions
Inactive
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 23, 2026 10:19 — with
GitHub Actions
Inactive
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 23, 2026 10:20 — with
GitHub Actions
Inactive
Copilot started reviewing on behalf of
Thota Sai Karthik (karthik-0306)
July 23, 2026 10:20
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Foundry agent-reference bug where RawFoundryAgentChatClient could inherit an unrelated model from OPENAI_*/AZURE_OPENAI_* environment variables and then send that model alongside agent_reference, causing Foundry to reject the request.
Changes:
- Force
RawFoundryAgentChatClient.modelto""afterRawOpenAIChatClientinitialization to prevent env-var model leakage. - Broaden model-stripping logic for agent-reference requests to treat empty-string models as “unset”.
- Add regression tests ensuring env-var models don’t pollute
client.model,Agent.default_options, or outbound payloads, while explicit model overrides still work.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/packages/foundry/agent_framework_foundry/_agent.py | Clears inherited model and updates stripping logic for agent-reference requests. |
| python/packages/foundry/tests/foundry/test_foundry_agent.py | Adds regression coverage for env-var model leakage and explicit model overrides. |
Comments suppressed due to low confidence (1)
python/packages/foundry/agent_framework_foundry/_agent.py:386
- When
self.modelis forced to "" (to avoid env-var leakage),Agent.__init__will still copy that empty string intodefault_options["model"](it adds the key whenever the value is not None). For continuation calls whereconversation_idis a standardresp_*/conv_*(soconversation_id is Noneis false),should_strip_modelbecomes false and the client can end up sendingmodel=""alongside the request. That likely produces an invalid/undesired payload; it would be safer to always drop a falsyrun_options["model"](""/None) regardless of conversation_id, while still preserving non-empty explicit models forresp_*continuity.
if not self.allow_preview:
extra_body.setdefault("agent_reference", _build_agent_reference(self.agent_name, self.agent_version))
should_strip_model = _uses_foundry_agent_session(conversation_id) or (
conversation_id is None and not options.get("model") # falsy catches None and "" (#7272)
)
if should_strip_model:
run_options.pop("model", None)
if extra_body:
Member
|
please use the defined PR template Thota Sai Karthik (@karthik-0306) |
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 23, 2026 12:13 — with
GitHub Actions
Inactive
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 23, 2026 12:25 — with
GitHub Actions
Inactive
Contributor
Author
Eduard van Valkenburg (@eavanvalkenburg) thanks for the note — updated the description to follow the template. |
Eduard van Valkenburg (eavanvalkenburg)
approved these changes
Jul 23, 2026
Eduard van Valkenburg (eavanvalkenburg)
enabled auto-merge
July 23, 2026 12:30
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
auto-merge was automatically disabled
July 23, 2026 13:24
Head branch was pushed to by a user without write access
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 23, 2026 13:24 — with
GitHub Actions
Inactive
Thota Sai Karthik (karthik-0306)
force-pushed
the
foundry-agent-env-model-fix
branch
from
July 24, 2026 01:00
cc499c9 to
3015e7f
Compare
Thota Sai Karthik (karthik-0306)
temporarily deployed
to
github-app-auth
July 24, 2026 01:00 — with
GitHub Actions
Inactive
Eduard van Valkenburg (eavanvalkenburg)
enabled auto-merge
July 30, 2026 09:57
Eduard van Valkenburg (eavanvalkenburg)
temporarily deployed
to
github-app-auth
July 30, 2026 09:57 — with
GitHub Actions
Inactive
Evan Mattson (moonbox3)
approved these changes
Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation & Context
RawOpenAIChatClient.__init__resolvesmodelfromOPENAI_CHAT_MODEL/OPENAI_MODELenvironment variables even when a pre-configuredasync_clientis passed.RawFoundryAgentChatClientinherits this, causingself.modelto get silently populated from an unrelated environment variable.Agent.__init__then copiesclient.modelintodefault_options["model"]. During request preparation,_prepare_options's strip condition (options.get("model") is None) never fired because the value was already a non-Nonestring — so an unrelated model was sent alongside the Foundryagent_reference, which the backend rejected because it must match the persisted agent's model.Description & Review Guide
What are the major changes?
RawFoundryAgentChatClient.__init__now explicitly setsself.model = ""aftersuper().__init__()— Foundry agent models are resolved server-side via the persisted agent definition, not from local env vars._prepare_options's strip condition fromoptions.get("model") is Nonetonot options.get("model")defensively to catch empty-string models while preserving explicit caller overrides (e.g.model="gpt-5.4").packages/foundry/tests/foundry/test_foundry_agent.py.What is the impact of these changes?
FoundryAgentandRawFoundryAgentChatClientfrom inheriting unrelatedOPENAI_CHAT_MODEL,OPENAI_MODEL, orAZURE_OPENAI_*environment variables.What do you want reviewers to focus on?
self.model = ""onRawFoundryAgentChatClientcleanly isolates client construction fromOPENAI_*env vars.default_options={"model": "gpt-5.4"}) survive unstripped.Related Issue
Fixes #7272
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.