Skip to content

Python: Prefer injected Cosmos vector store clients over connection settings - #9996

Open
Sumedh Aerram (sumedh-aerram) wants to merge 4 commits into
microsoft:mainfrom
sumedh-aerram:fix/python-cosmos-injected-client
Open

Sumedh Aerram (sumedh-aerram) wants to merge 4 commits into
microsoft:mainfrom
sumedh-aerram:fix/python-cosmos-injected-client

Conversation

@sumedh-aerram

@sumedh-aerram Sumedh Aerram (sumedh-aerram) commented Nov 2, 2026 •

Copy link
Copy Markdown

Motivation & Context

CosmosStore and CosmosCollection raise when a caller injects a CosmosClient, DatabaseProxy, or ContainerProxy together with endpoint, credential, key, or .env options, even though those settings only matter when the connector creates its own client. An account-level CosmosClient also required an explicit database_name and could not read it from the environment.

Description & Review Guide

  • What are the major changes? An injected Cosmos client or proxy now takes precedence over endpoint, credential, key, and .env options, which are ignored instead of rejected. create_database is ignored for injected database and container proxies (previously rejected); with an injected account client it still creates the database on first use, as before. Resource names the injected object does not provide resolve as explicit argument > AZURE_COSMOS_DATABASE_NAME > a focused error for the database (account client), and explicit argument > the model's registered collection name > AZURE_COSMOS_CONTAINER_NAME > a focused error for the container (account or database client). An explicit name that conflicts with an injected proxy's id still raises (collection_name for container_client, and now also database_name for database_client).
  • What is the impact of these changes? Constructors that previously raised for redundant connection settings now accept them. Rejecting multiple injected clients, keeping injected clients open on close, and the owned-client path (no injection) are unchanged.
  • What do you want reviewers to focus on? Name resolution for injected clients in _resolve_resource_name, which reads only the process environment because .env options are ignored when a client is injected, the conflict checks for explicit names against an injected proxy's id, and the registered-definition lookup through core's _VECTOR_MODEL_REGISTRY (core has no public accessor; happy to switch if you prefer another approach).

Tests cover each acceptance case in the issue: injected account, database, and container clients bypassing explicit settings and missing or undecodable .env files; explicit > environment > error name resolution; mismatched and matching explicit names; registered (undecorated) models keeping their collection name; injected clients staying open after close; and unchanged owned-client settings resolution and close. The new acceptance tests failed against main (18 failures) and pass with the change. Locally (macOS, Python 3.13): azure-cosmos package tests 228 passed, 3 integration tests skipped; ruff format/ruff check, poe pyright -P azure-cosmos, and poe mypy -P azure-cosmos pass.

Related Issue

Fixes #9955

No other open PR references this issue.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: implementation, tests, review, and this PR description.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

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

Collection-name lookup misses non-decorated registered models, and injected Cosmos clients can still create databases despite the option being documented as ignored.

2 open issues
What changed in this PR

Updates the Python Cosmos vector-store connector to prefer caller-injected SDK clients over connection settings, addressing #9955 while preserving caller ownership.

Changes:

  • Accepts redundant connection settings when a client or proxy is injected.
  • Adds environment fallback for resource names and explicit-name conflict checks for database proxies.
  • Expands tests for settings precedence, injected-client lifetime, and unchanged owned-client behavior.
File Description
python/​packages/​azure-cosmos/​tests/​azure_cosmos/​test_vector_store.py Adds coverage for injection, name resolution, conflicts, and client ownership.
python/​packages/​azure-cosmos/​agent_framework_azure_cosmos/​_vector_store.py Relaxes injection restrictions and resolves remaining resource names independently.

Review effort: Balanced

This branch was successfully deployed

1 active deployment
github-app-auth — 40e27dd3 Deployed Nov 3, 2026 by sumedh-aerram via add_label #27020
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]: Azure Cosmos vector stores reject settings instead of preferring injected clients

2 participants