Python: fix FoundryChatClient/FoundryAgent dropping default_headers#5433
Python: fix FoundryChatClient/FoundryAgent dropping default_headers#5433he-yufeng wants to merge 1 commit intomicrosoft:mainfrom
Conversation
default_headers was accepted by the constructor and stored on the instance but never forwarded to the AsyncOpenAI client obtained from AIProjectClient.get_openai_client(), so custom headers never reached outbound requests. Forward default_headers through get_openai_client(**kwargs) -- the Azure SDK already supports this kwarg and passes it on to AsyncOpenAI. Added two regression tests and updated the mock-based header test so we cover both the stored-attribute path and the actually-forwarded path. Fixes microsoft#5416.
Python Test Coverage Report •
Python Unit Test Overview
|
|||||||||||||||||||||||||||||||||||
|
Thanks for the approval @TaoChenOSU! The screenshot doesn't render for me in the API — just to make sure I'm removing the right thing, are you suggesting we also drop the now-redundant If that's the intent, I'll push a follow-up that also removes |
@he-yufeng please accept the CLA |
|
Closing this, as it is already done, with additional setup for the new hosted agents in #5447 |

Motivation and Context
Fixes #5416.
FoundryChatClient(default_headers=...)andFoundryAgent(default_headers=...)are documented to set additional HTTP headers on outbound requests. The constructor accepted the parameter, stored it onself.default_headers, and then forgot about it — the underlyingAsyncOpenAIclient was obtained viaproject_client.get_openai_client()without forwarding the headers, so custom headers never reached the wire.This is a silent failure: the parameter looks wired up, existing unit test (
test_init_with_default_header) even asserts the stored attribute is populated, but the reporter still spent hours tracking down why their header wasn't appearing in requests.Description
AIProjectClient.get_openai_client(**kwargs)already forwardsdefault_headers(and friends) toAsyncOpenAI, so the fix is just to pass them through from the two Foundry entry points:agent_framework_foundry/_chat_client.py(RawFoundryChatClient.__init__)agent_framework_foundry/_agent.py(FoundryAgent.__init__)I deliberately didn't touch
load_openai_service_settingsin the sharedopenaipackage — while that is where the headers are dropped for the pre-built-client path, the reported bug is Foundry-specific and going throughget_openai_clientis the Azure-native way. The existingself.default_headersstorage on the instance is kept intact for serialization/reflection.Tests
test_default_headers_forwarded_to_openai_client— regression test: asserts the headers actually land on theget_openai_clientcall.test_get_openai_client_not_called_with_headers_kwarg_when_unset— don't inject an empty dict when the user didn't ask for headers.test_init_with_default_headerstill passes (storage attribute behavior unchanged).Full foundry test suite:
102 passed, 14 skipped(skips are the integration tests needing a real endpoint).Contribution Checklist