Repository navigation
Python: keep Foundry injected clients caller-owned on cleanup - #10000
NingLin Liu (ninglinLiu) wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Direct imports of typing.Self break package imports and test collection on supported Python 3.10, and two existing lifecycle tests fail because their expectations no longer match injected-client behavior.
3 open issues
What changed in this PR
This PR addresses #9964 by preserving caller ownership of injected SDK clients in the Python Foundry embedding and memory integrations.
Changes:
- Tracks ownership of embedding transports and closes only wrapper-owned clients.
- Limits memory-provider context management to internally created project clients.
- Documents ownership behavior and adds injected-client lifecycle regression tests.
| File | Description |
|---|---|
python/packages/foundry/tests/foundry/test_foundry_injected_client_lifecycle.py |
Adds tests that injected clients are not closed or entered/exited. |
python/packages/foundry/agent_framework_foundry/_memory_provider.py |
Guards project-client lifecycle calls by ownership and documents the contract. |
python/packages/foundry/agent_framework_foundry/_embedding_client.py |
Tracks text/image client ownership and guards cleanup accordingly. |
Review effort: Balanced
| import sys | ||
| from contextlib import AbstractAsyncContextManager | ||
| from typing import TYPE_CHECKING, Any, ClassVar | ||
| from typing import TYPE_CHECKING, Any, ClassVar, Self, TypedDict |
| clients remain caller-owned. | ||
| """ | ||
| if ( | ||
| self._owns_project_client |
| from typing import Any, Self | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest |
… tests Keep the version-gated typing_extensions Self and TypedDict import so FoundryMemoryProvider loads on Python 3.10, import Self from typing_extensions in the injected-client lifecycle tests, and construct the aenter/aexit delegation tests through a patched AIProjectClient. Co-authored-by: NingLin Liu <x07514475479@gmail.com>
Import Self from typing so pyupgrade --py311-plus leaves the lifecycle test unchanged, and assert injected project-client entry and exit against patched AsyncMocks. Co-authored-by: NingLin Liu <x07514475479@gmail.com>
Muhammad Abdullah Butt (1AbdullahButt)
left a comment
There was a problem hiding this comment.
The ownership rule is right, and #9964 is still open on main: RawFoundryEmbeddingClient.close() on main still closes _text_client/_image_client unconditionally. But this branch now conflicts with main in _embedding_client.py, and main changed in a way that affects where the fix belongs.
What changed on main. #9991 (merged since this branch was cut, about 200 commits ago) reworked the constructor. Text and image clients are now created lazily in _ensure_text_client() and _ensure_image_client() (_embedding_client.py:403-430 on main). The constructor creates both eagerly only when neither client is injected (:322-324). So on main, one instance can hold an injected text_client and an image_client it creates on the first image request. Ownership has to be tracked per client, at the point of creation. git merge-tree reports the content conflict in that file. _memory_provider.py merges cleanly.
How to rebase it. Leave _owns_text_client/_owns_image_client False in __init__, and set each one to True next to self._text_client = created / self._image_client = created in the _ensure_* methods. Then close() can keep the guards this PR adds:
def _ensure_text_client(self) -> EmbeddingsClient:
...
created = EmbeddingsClient(**client_kwargs, per_retry_policies=[create_feature_usage_policy()])
self._text_client = created
self._owns_text_client = True
return createdSetting the flags from text_client is None in __init__, as the branch does now, would also mark a client "owned" that is never created. That's harmless because of the is not None check in close(), but it's misleading. And it would only be right on main by accident.
A test worth adding after the rebase: inject only text_client, configure an endpoint and credential, call get_embeddings with an image input so that _ensure_image_client() creates the image client, then call close(). The image client should be closed and the injected text client shouldn't be.
FoundryMemoryProvider looks right. owns_project_client is set only on the AIProjectClient(**project_client_kwargs) branch, and __aenter__/__aexit__ (_memory_provider.py:150-174) only enter and exit an owned client. The other uses of project_client (beta.memory_stores.*) don't depend on it having been entered. The updated tests in test_foundry_memory_provider.py assert that an injected client is never entered or exited.
I didn't run the tests.
Reviewed with help from Claude Code.

Fixes #9964
Description
RawFoundryEmbeddingClient.close()no longer closes caller-suppliedtext_client/image_client.FoundryMemoryProviderno longer enters/exits an injectedproject_client. Only clients created by the wrappers are lifecycle-managed.Tests
Added focused unit tests for injected embedding and memory client ownership.
AI assistance: used an AI coding assistant to implement and draft this change. I reviewed the diff.