Skip to content

Conversation

@keyurva
Copy link
Contributor

@keyurva keyurva commented Jan 9, 2026

  • Adds ause_api_key context manager to override the API key for specific request scopes.
  • Enables using different keys without re-instantiating the client, useful for multi-tenant applications (like hosted mcp servers).
  • Ensures the overridden key is correctly propagated even for methods that rely on worker threads (e.g. node.fetch_place_ancestors).
from datacommons_client import use_api_key

# Standard usage (uses client default key)
client.observation.fetch(...)

# Usage with override
with use_api_key("new-scoped-key"):
    client.observation.fetch(...)
    # Works for threaded methods too
    client.node.fetch_place_ancestors(...)

@keyurva keyurva requested review from clincoln8 and gmechali January 9, 2026 20:32
@gemini-code-assist
Copy link

Summary of Changes

Hello @keyurva, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the datacommons_client by introducing a flexible way to manage API keys at request-time. A new use_api_key context manager allows users to temporarily override the default API key for specific blocks of code, which is particularly useful for multi-tenant applications or scenarios requiring dynamic key switching. The implementation ensures that this contextual API key is correctly applied across all client operations, including those that leverage multi-threading, by integrating Python's contextvars module.

Highlights

  • New Context Manager: Introduced a use_api_key context manager that allows overriding the API key for specific request scopes without re-instantiating the client.
  • API Key Propagation: Implemented mechanisms to ensure the overridden API key is correctly propagated to all HTTP requests, including those made within worker threads (e.g., node.fetch_place_ancestors).
  • Context Variable Integration: Utilized Python's contextvars module to manage and propagate the API key across different execution contexts, including those involving ThreadPoolExecutor.
  • Comprehensive Testing: Added new unit and integration tests to validate the functionality of the use_api_key context manager for both single-threaded and multi-threaded API calls, covering basic usage, nesting, and edge cases.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a use_api_key context manager to allow overriding the API key for specific requests. This is a valuable feature for multi-tenant applications. The implementation correctly uses contextvars to manage the key, ensuring it's propagated to worker threads for asynchronous operations. The changes are well-tested, with new unit tests for the context manager and integration tests for both synchronous and threaded client methods.

My review includes a couple of suggestions to improve code style and consistency:

  • Moving a local import to the top of the file to adhere to PEP 8.
  • Aligning the testing style in a new test file with the existing pytest conventions for better project consistency.

Overall, this is a great addition to the client library.

Copy link
Contributor

@clincoln8 clincoln8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool, thanks Keyur!

Are you planning on publishing to TestPyPi first?
Will we update the python v2 docs to include info about this option?

Copy link
Contributor Author

@keyurva keyurva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

Are you planning on publishing to TestPyPi first?

Yes. Will follow the release process in https://github.com/datacommonsorg/api-python/blob/master/docs/release.md which includes publishing to TestPyPi

Will we update the python v2 docs to include info about this option?

Hmm - not sure if users of the library will be using this option so not sure about updating docs either.

WDYT? Also, @kmoscoe - thoughts?

@keyurva keyurva merged commit e39f887 into datacommonsorg:master Jan 11, 2026
3 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.

2 participants