Python: Document shared chat client concurrency - #7680
Conversation
|
Ricky-7-Yan please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
2 similar comments
|
Ricky-7-Yan please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
|
Ricky-7-Yan please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR clarifies and validates the concurrency contract for the OpenAI and Foundry chat clients, documenting when a single client instance can be safely reused across concurrent async runs and adding a regression test to ensure concurrent runs remain isolated.
Changes:
- Documented concurrency/reuse guarantees (and non-guarantees) for
OpenAIChatClientandFoundryChatClientin docstrings and READMEs. - Added guidance to the core chat protocol docs that protocol conformance doesn’t imply concurrency safety.
- Added an async concurrency isolation test covering streaming, non-streaming, and mixed scenarios for both providers.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| python/packages/openai/agent_framework_openai/_chat_client.py | Adds explicit concurrency contract to OpenAIChatClient docstring. |
| python/packages/openai/README.md | Documents concurrent reuse guidance for OpenAI package users. |
| python/packages/foundry/agent_framework_foundry/_chat_client.py | Adds explicit concurrency contract to FoundryChatClient docstring. |
| python/packages/foundry/README.md | Documents concurrent reuse guidance for Foundry package users. |
| python/packages/core/agent_framework/_clients.py | Clarifies that protocol implementation doesn’t guarantee concurrency safety. |
| python/packages/foundry/tests/foundry/test_chat_client_concurrency.py | Adds regression test ensuring concurrent runs don’t leak state across requests/sessions. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| An `OpenAIChatClient` instance can be shared by concurrent asynchronous calls on the same event loop. Streaming, | ||
| non-streaming, and mixed calls are supported. Keep mutable run state isolated by creating a separate `Agent` and | ||
| `AgentSession` for each concurrent run and by passing separate messages and options. |
| if self.active_requests == 2: | ||
| self._both_calls_started.set() | ||
| try: | ||
| await asyncio.wait_for(self._both_calls_started.wait(), timeout=1) |
|
|
||
| async def __call__(self, request: httpx.Request) -> httpx.Response: | ||
| body = request.content.decode() | ||
| marker = next(marker for marker in _MARKERS if marker in body) |
Motivation & Context
Long-lived async hosts need to reuse provider clients and their HTTP connection pools without leaking request-scoped Agent or session state. The Python API did not define whether
OpenAIChatClientandFoundryChatClientsupport concurrent reuse, especially when streaming and non-streaming calls overlap.Description & Review Guide
Agent,AgentSession, message set, and options request-scoped. There is no runtime or public API behavior change.Related Issue
Fixes #7654
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.