Skip to content

Python: Fix FoundryAgent inheriting OPENAI_CHAT_MODEL for agent-reference requests - #7283

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 4 commits into
microsoft:mainfrom
karthik-0306:foundry-agent-env-model-fix
Jul 30, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 4 commits into
microsoft:mainfrom
karthik-0306:foundry-agent-env-model-fix

Conversation

@karthik-0306

@karthik-0306 Karthik Thota (karthik-0306) commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

RawOpenAIChatClient.__init__ resolves model from OPENAI_CHAT_MODEL/OPENAI_MODEL environment variables even when a pre-configured async_client is passed. RawFoundryAgentChatClient inherits this, causing self.model to get silently populated from an unrelated environment variable.

Agent.__init__ then copies client.model into default_options["model"]. During request preparation, _prepare_options's strip condition (options.get("model") is None) never fired because the value was already a non-None string — so an unrelated model was sent alongside the Foundry agent_reference, which the backend rejected because it must match the persisted agent's model.

Description & Review Guide

  • What are the major changes?

    1. RawFoundryAgentChatClient.__init__ now explicitly sets self.model = "" after super().__init__() — Foundry agent models are resolved server-side via the persisted agent definition, not from local env vars.
    2. Updated _prepare_options's strip condition from options.get("model") is None to not options.get("model") defensively to catch empty-string models while preserving explicit caller overrides (e.g. model="gpt-5.4").
    3. Added regression tests in packages/foundry/tests/foundry/test_foundry_agent.py.
  • What is the impact of these changes?

    • Prevents FoundryAgent and RawFoundryAgentChatClient from inheriting unrelated OPENAI_CHAT_MODEL, OPENAI_MODEL, or AZURE_OPENAI_* environment variables.
    • Ensures clean payloads for agent-reference requests.
  • What do you want reviewers to focus on?

    • Verify that self.model = "" on RawFoundryAgentChatClient cleanly isolates client construction from OPENAI_* env vars.
    • Confirm that explicit caller model overrides (e.g., via default_options={"model": "gpt-5.4"}) survive unstripped.

Related Issue

Fixes #7272

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

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.

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.model to "" after RawOpenAIChatClient initialization 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.model is forced to "" (to avoid env-var leakage), Agent.__init__ will still copy that empty string into default_options["model"] (it adds the key whenever the value is not None). For continuation calls where conversation_id is a standard resp_*/conv_* (so conversation_id is None is false), should_strip_model becomes false and the client can end up sending model="" alongside the request. That likely produces an invalid/undesired payload; it would be safer to always drop a falsy run_options["model"] (""/None) regardless of conversation_id, while still preserving non-empty explicit models for resp_* 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:

@eavanvalkenburg

Copy link
Copy Markdown
Member

please use the defined PR template Karthik Thota (@karthik-0306)

@karthik-0306

Copy link
Copy Markdown
Contributor Author

please use the defined PR template Karthik Thota (@karthik-0306)

Eduard van Valkenburg (@eavanvalkenburg) thanks for the note — updated the description to follow the template.

@github-actions

github-actions Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/foundry/agent_framework_foundry
   _agent.py2624084%119, 122, 244–245, 249–251, 256–259, 359, 467–468, 492, 495, 506–518, 522–523, 721–722, 725, 751, 761, 783, 871, 876, 880
TOTAL46057447490% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9540 34 💤 0 ❌ 0 🔥 1m 58s ⏱️

auto-merge was automatically disabled July 23, 2026 13:24

Head branch was pushed to by a user without write access

Merged via the queue into microsoft:main with commit 4d67eef Jul 30, 2026
37 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — d8a625b6 Deployed Jul 30, 2026 by eavanvalkenburg via add_label #19963
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]: FoundryAgent inherits unrelated OPENAI_CHAT_MODEL for agent-reference requests

4 participants