Skip to content

Allow injecting embeddings and chat clients#27

Merged
aayush3011 merged 1 commit into
AzureCosmosDB:mainfrom
TheovanKraay:feature/injectable-embeddings-chat-clients
Jul 1, 2026
Merged

Allow injecting embeddings and chat clients#27
aayush3011 merged 1 commit into
AzureCosmosDB:mainfrom
TheovanKraay:feature/injectable-embeddings-chat-clients

Conversation

@TheovanKraay

Copy link
Copy Markdown
Contributor

Summary

Adds optional embeddings_client and chat_client constructor arguments to both
CosmosMemoryClient and AsyncCosmosMemoryClient. When supplied, the toolkit uses
the injected client instead of constructing an Azure-backed one, and treats the
caller as the owner of its lifecycle (so close() does not close it). When omitted,
behavior is unchanged: the toolkit builds and owns the Azure OpenAI / AI Foundry
clients exactly as before.

Motivation

Today the embeddings and chat clients are hard-constructed inside __init__, with no
supported way to supply your own. That couples the toolkit to Azure OpenAI / AI Foundry
and forces anyone who needs a different backend (or offline/deterministic behavior) to
reach into private internals (_embeddings_client / _chat_client). This adds a clean,
public seam instead.

Use cases this enables:

  • Pluggable model backends — OpenAI-compatible or self-hosted embedding/chat
    services (via an OpenAI-compatible client), gateways, or alternate providers.
  • Reuse of a caller-configured client — apps that already build a client with
    custom retry/timeout/proxy/telemetry can hand the same instance to the toolkit
    instead of it creating a second, differently-configured one.
  • Deterministic, offline testing — inject fakes to exercise the pipeline and the
    Cosmos vector path (e.g. against the Cosmos DB emulator) without any Azure account,
    and without monkeypatching private internals.

Changes

  • CosmosMemoryClient.__init__ and AsyncCosmosMemoryClient.__init__ accept
    embeddings_client and chat_client (both default None).
  • Ownership tracked via _owns_embeddings_client / _owns_chat_client, mirroring the
    existing _owns_*_credential pattern. close() only closes clients the toolkit built.
  • Unit tests (sync + async) covering: injected clients are used, ownership flags,
    independent injection of one client, and that close() does not close injected clients.
  • CHANGELOG entry under Unreleased.

Compatibility

Fully backward compatible. Both arguments default to None; existing callers get the
current Azure-backed clients and lifecycle with no changes.

Testing

  • tests/unit/test_cosmos_memory_client.py and tests/unit/aio/test_cosmos_memory_client.py
    pass (128 tests across both suites).
  • ruff check and ruff format --check clean on the changed files.

Add optional embeddings_client and chat_client constructor arguments to
CosmosMemoryClient and AsyncCosmosMemoryClient. When provided, the toolkit
uses the injected client instead of building an Azure-backed one and does not
close it (the caller owns its lifecycle, mirroring credential ownership).

Enables OpenAI-compatible/self-hosted embedding and chat backends, reuse of a
caller-configured client, and deterministic offline testing. Adds sync and
async unit tests covering injection, ownership flags, and close() behavior.
Copilot AI review requested due to automatic review settings July 1, 2026 12:16

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.

Pull request overview

Adds a supported dependency-injection seam for the model-layer clients used by the Cosmos memory clients, allowing callers to supply their own embeddings/chat backends (or fakes) while preserving existing default Azure-backed behavior and lifecycle semantics.

Changes:

  • Add optional embeddings_client and chat_client constructor args to CosmosMemoryClient and AsyncCosmosMemoryClient, with ownership flags controlling whether close() tears them down.
  • Update close() implementations to only close clients constructed by the toolkit.
  • Add sync/async unit tests and a CHANGELOG entry documenting the new injection capability.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
azure/cosmos/agent_memory/cosmos_memory_client.py Adds injectable embeddings/chat clients and ownership-aware closing for the sync client.
azure/cosmos/agent_memory/aio/cosmos_memory_client.py Adds injectable embeddings/chat clients and ownership-aware closing for the async client.
tests/unit/test_cosmos_memory_client.py Adds sync unit tests verifying injection behavior and lifecycle ownership.
tests/unit/aio/test_cosmos_memory_client.py Adds async unit tests verifying injection behavior and lifecycle ownership.
CHANGELOG.md Documents the new injection feature under Unreleased.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@aayush3011 aayush3011 merged commit 4d9a471 into AzureCosmosDB:main Jul 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants