Skip to content

Python: Prefer injected MongoDB clients over connection settings - #9982

Open
ZedingZhang (ZedingZhang) wants to merge 1 commit into
microsoft:mainfrom
ZedingZhang:fix/mongodb-injected-client-settings-9952
Open

ZedingZhang (ZedingZhang) wants to merge 1 commit into
microsoft:mainfrom
ZedingZhang:fix/mongodb-injected-client-settings-9952

Conversation

@ZedingZhang

@ZedingZhang ZedingZhang (ZedingZhang) commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

MongoDBStore and MongoDBCollection reject connection settings alongside an injected async_client, and require an explicit database name even when MONGODB_DATABASE_NAME is set. This prevents callers from reusing their configured client with shared connection configuration.

Description & Review Guide

  • What are the major changes? Use the injected client without loading URI, app-name, or .env settings. Resolve its database selector independently from an explicit database_name, then MONGODB_DATABASE_NAME. Update the constructor documentation and README, and extend the existing settings tests for both stores and collections.
  • What is the impact of these changes? Redundant connection settings no longer reject an injected client, and environment-based database selection works with that client. A valid database name is still required.
  • What do you want reviewers to focus on? The database-only settings schema prevents transport configuration from being loaded for injected clients. Tests verify that the SDK factory is not called and wrapper cleanup leaves injected clients open. Twelve acceptance cases fail on the original implementation and pass with the fix.

Related Issue

Fixes #9952. The issue has the ready-for-implementation label; no competing open PR was found before submission.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution.

AI assistance details: Codex assisted with investigation, implementation, tests, documentation, and this PR description. Codex executed the reported checks; I reviewed all submitted content and take responsibility for it.

Contribution Checklist

  • The code builds clean without any errors or warnings — MongoDB sdist and wheel built successfully.
  • All unit tests pass, and I have added new tests where possible — MongoDB unit suite: 119 passed, 5 integration tests deselected. Ruff formatting/lint, strict source Pyright, all five test type checkers, and README code-block checks pass. No live MongoDB integration tests were run.
  • 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.

🟢 Approval recommended

No active review issues block the injected MongoDB client and database-selection changes.

No open issues 🎉

What changed in this PR

This PR addresses #9952 by allowing MongoDB stores and collections to reuse an injected client despite redundant connection settings, while resolving the required database name independently.

Changes:

  • Ignore URI, app-name, and .env options for injected clients; resolve the database name from an explicit argument, then MONGODB_DATABASE_NAME.
  • Extend tests for database precedence, ignored settings, skipped client creation, and caller-owned client cleanup.
  • Update documentation to describe the injected-client behavior.
File Description
python/​packages/​mongodb/​tests/​mongodb/​test_settings.py Tests injected-client settings, database resolution, and ownership.
python/​packages/​mongodb/​README.md Documents database selection and ignored connection settings.
python/​packages/​mongodb/​agent_framework_mongodb/​_vector_store.py Adds database-only settings resolution and updates constructor documentation.

Review effort: Balanced

This branch was previously deployed

1 inactive deployment
github-app-auth — fece7385 Deployed Nov 2, 2026 by ZedingZhang via add_label #26863
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: MongoDB vector stores reject settings instead of preferring an injected client

2 participants