Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/packages/foundry/agent_framework_foundry/_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ def __init__(
self._should_close_client = True

# Get OpenAI client from project
async_client = self.project_client.get_openai_client()
openai_client_kwargs: dict[str, Any] = {}
if default_headers:
openai_client_kwargs["default_headers"] = dict(default_headers)
async_client = self.project_client.get_openai_client(**openai_client_kwargs)

super().__init__(
async_client=async_client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ def __init__(
project_client_kwargs["allow_preview"] = allow_preview
project_client = AIProjectClient(**project_client_kwargs)

openai_client_kwargs: dict[str, Any] = {}
if default_headers:
openai_client_kwargs["default_headers"] = dict(default_headers)

super().__init__(
model=resolved_model,
async_client=project_client.get_openai_client(),
async_client=project_client.get_openai_client(**openai_client_kwargs),
default_headers=default_headers,
instruction_role=instruction_role,
compaction_strategy=compaction_strategy,
Expand Down
37 changes: 37 additions & 0 deletions python/packages/foundry/tests/foundry/test_foundry_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,43 @@ def test_init_with_default_header() -> None:
assert client.default_headers[key] == value


def test_default_headers_forwarded_to_openai_client() -> None:
"""Regression: ``default_headers`` must be forwarded to the underlying AsyncOpenAI client.

Previously, ``FoundryChatClient(default_headers=...)`` stored the headers on the instance
but called ``project_client.get_openai_client()`` without forwarding them, so the headers
never reached outbound HTTP requests.
"""
default_headers = {"x-custom-header": "repro-value"}
mock_openai_client = _make_mock_openai_client()
project_client = MagicMock()
project_client.get_openai_client.return_value = mock_openai_client

FoundryChatClient(
project_client=project_client,
model=_TEST_FOUNDRY_MODEL,
default_headers=default_headers,
)

project_client.get_openai_client.assert_called_once()
forwarded = project_client.get_openai_client.call_args.kwargs.get("default_headers")
assert forwarded is not None
for key, value in default_headers.items():
assert forwarded.get(key) == value


def test_get_openai_client_not_called_with_headers_kwarg_when_unset() -> None:
"""When ``default_headers`` is not passed, don't inject an empty dict into the Azure call."""
mock_openai_client = _make_mock_openai_client()
project_client = MagicMock()
project_client.get_openai_client.return_value = mock_openai_client

FoundryChatClient(project_client=project_client, model=_TEST_FOUNDRY_MODEL)

project_client.get_openai_client.assert_called_once()
assert "default_headers" not in project_client.get_openai_client.call_args.kwargs


def test_init_with_project_endpoint_creates_project_client() -> None:
credential = MagicMock()
mock_openai_client = _make_mock_openai_client()
Expand Down