Skip to content

Python: keep Foundry injected clients caller-owned on cleanup - #10000

Open
NingLin Liu (ninglinLiu) wants to merge 3 commits into
microsoft:mainfrom
ninglinLiu:fix/9964-foundry-injected-client-lifecycle
Open

NingLin Liu (ninglinLiu) wants to merge 3 commits into
microsoft:mainfrom
ninglinLiu:fix/9964-foundry-injected-client-lifecycle

Conversation

@ninglinLiu

Copy link
Copy Markdown
Contributor

Fixes #9964

Description

RawFoundryEmbeddingClient.close() no longer closes caller-supplied text_client / image_client. FoundryMemoryProvider no longer enters/exits an injected project_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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
Comment on lines +6 to +9
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>
@cursor
cursor Bot temporarily deployed to github-app-auth November 3, 2026 06:54 Inactive
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>
@cursor
cursor Bot temporarily deployed to github-app-auth November 3, 2026 08:30 Inactive

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 created

Setting 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.

This branch was previously deployed

1 inactive deployment
github-app-auth — edb6609f Deployed Nov 3, 2026 by cursor[bot] via add_label #26924
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Foundry embedding and memory integrations close caller-owned clients

4 participants