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

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

3 participants