Repository navigation
Python: Prefer injected DocumentDB clients over connection settings - #9999
Yufeng He (he-yufeng) wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Missing-selector errors reference a nonexistent constructor argument, and precedence tests leave registered-model collection names untested against conflicting environment settings.
2 open issues
What changed in this PR
This PR addresses #9954 by making the Python DocumentDB connector prefer caller-injected PyMongo objects over connection settings while preserving caller ownership.
Changes:
- Separates resource-selector resolution from client-construction settings.
- Adds environment fallback for unresolved selectors, with model-defined collection names taking precedence.
- Documents injection behavior and adds regression tests for precedence, validation, and cleanup.
| File | Description |
|---|---|
python/packages/azure-documentdb/tests/azure_documentdb/test_settings.py |
Adds injection, selector, name-mismatch, and ownership regression coverage. |
python/packages/azure-documentdb/README.md |
Explains injected-object precedence and selector fallback. |
python/packages/azure-documentdb/agent_framework_azure_documentdb/_vector_store.py |
Updates injection handling, isolates selector settings, and documents constructor behavior. |
Review effort: Balanced
| class _AzureDocumentDBResourceSettings(TypedDict, total=False): | ||
| name: str | None | ||
|
|
||
|
|
||
| def _resolve_resource_name(value: str | None, kind: str) -> str: | ||
| if value is None: | ||
| settings = load_settings( | ||
| _AzureDocumentDBResourceSettings, | ||
| env_prefix=f"AZURE_DOCUMENTDB_{kind.upper()}_", | ||
| required_fields=["name"], | ||
| ) | ||
| value = settings.get("name") | ||
| if kind == "database": | ||
| return _validate_database_name(value) | ||
| return _validate_collection_name(value) |
There was a problem hiding this comment.
Split the selector-only settings into database and collection schemas. Missing-selector errors now identify the actual database_name or collection_name constructor argument as well as the corresponding environment variable. The regression asserts both parts of the message for account-client, collection-client, and database injection paths.
| registration = _VECTOR_MODEL_REGISTRY.get(record_type) | ||
| registered_definition = definition or (registration.definition if registration is not None else None) |
There was a problem hiding this comment.
Added a registered-model regression with no definition argument and a conflicting AZURE_DOCUMENTDB_COLLECTION_NAME, parameterized over both injected account-client and injected-database paths. The collection name comes from the registration in both cases. The full connector suite now has 129 passed, 1 skipped.
…b-injected-client-precedence
…b-injected-client-precedence

Motivation & Context
AzureDocumentDBStoreandAzureDocumentDBCollectionreject caller-supplied PyMongo clients, databases, and collections whenever redundant connection settings are present. They also require an explicit database name for an account-level injected client instead of using the existing environment selector. This prevents configuration layers from injecting a fully configured object while still forwarding unrelated construction settings.Fixes #9954, which is labelled
ready-for-implementationand defines the injection, selector, and ownership acceptance cases. No competing open PR exists for this issue.Description & Review Guide
.envfiles, and ambient connection settings. Account clients resolve a missing database selector fromAZURE_DOCUMENTDB_DATABASE_NAME. Collections created from an injected client or database resolve a missing collection selector from an explicit argument, the registered model definition, thenAZURE_DOCUMENTDB_COLLECTION_NAME. Resource settings are loaded separately, so connection parsing and validation cannot leak back into these paths.database_nameorcollection_nameconstructor argument. On final headedc158364f, the complete connector suite is 129 passed, 1 skipped; mypy and zuban report no issues in the four test source files, Pyright reports zero source errors, and Ruff passes.Related Issue
Fixes #9954
AI Assistance
AI assistance details: implementation, tests, documentation, and this PR description. The author reviewed the final diff, ran the base-to-fixed regression controls and connector suite, and checked Ruff, Pyright, and repository hooks locally.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix); a workflow keeps the label and title prefix in sync automatically.