-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Python: make FoundryChatClient an async context manager #5464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Yufeng He (he-yufeng)
wants to merge
5
commits into
microsoft:main
from
he-yufeng:fix/foundry-chat-client-async-context
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f282f15
Python: make FoundryChatClient an async context manager
he-yufeng bf11675
test: cover FoundryChatClient context ownership
he-yufeng f423efa
fix: close owned Foundry client on init failure
he-yufeng 2037ee3
fix: keep async cleanup task referenced
he-yufeng 00e4a76
Merge remote-tracking branch 'upstream/main' into fix/foundry-chat-cl…
he-yufeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
python/packages/foundry/tests/foundry/test_foundry_chat_client_context_manager.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from agent_framework_foundry import FoundryChatClient | ||
|
|
||
| _TEST_FOUNDRY_PROJECT_ENDPOINT = "https://test-project.services.ai.azure.com/" | ||
| _TEST_FOUNDRY_MODEL = "test-gpt-4o" | ||
|
|
||
|
|
||
| def _make_mock_openai_client() -> MagicMock: | ||
| client = MagicMock() | ||
| client.default_headers = {} | ||
| client.responses = MagicMock() | ||
| client.responses.create = AsyncMock() | ||
| client.responses.parse = AsyncMock() | ||
| return client | ||
|
|
||
|
|
||
| async def test_context_manager_closes_owned_project_client() -> None: | ||
| credential = MagicMock() | ||
| project_client = MagicMock() | ||
| project_client.get_openai_client.return_value = _make_mock_openai_client() | ||
| project_client.close = AsyncMock() | ||
|
|
||
| with patch("agent_framework_foundry._chat_client.AIProjectClient", return_value=project_client): | ||
| async with FoundryChatClient( | ||
| project_endpoint=_TEST_FOUNDRY_PROJECT_ENDPOINT, | ||
| model=_TEST_FOUNDRY_MODEL, | ||
| credential=credential, | ||
| ) as client: | ||
| assert client.project_client is project_client | ||
|
|
||
| project_client.close.assert_awaited_once_with() | ||
|
|
||
|
|
||
| async def test_context_manager_does_not_close_injected_project_client() -> None: | ||
| project_client = MagicMock() | ||
| project_client.get_openai_client.return_value = _make_mock_openai_client() | ||
| project_client.close = AsyncMock() | ||
|
|
||
| async with FoundryChatClient(project_client=project_client, model=_TEST_FOUNDRY_MODEL) as client: | ||
| assert client.project_client is project_client | ||
|
|
||
| project_client.close.assert_not_awaited() | ||
|
|
||
|
|
||
| async def test_constructor_closes_owned_project_client_when_openai_client_creation_fails() -> None: | ||
| credential = MagicMock() | ||
| project_client = MagicMock() | ||
| project_client.get_openai_client.side_effect = RuntimeError("boom") | ||
| project_client.close = AsyncMock() | ||
|
|
||
| with ( | ||
| patch("agent_framework_foundry._chat_client.AIProjectClient", return_value=project_client), | ||
| pytest.raises(RuntimeError, match="boom"), | ||
| ): | ||
| FoundryChatClient( | ||
| project_endpoint=_TEST_FOUNDRY_PROJECT_ENDPOINT, | ||
| model=_TEST_FOUNDRY_MODEL, | ||
| credential=credential, | ||
| ) | ||
|
|
||
| await asyncio.sleep(0) | ||
| project_client.close.assert_awaited_once_with() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this handled?