-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(tools): support additional headers for google api toolset #non-breaking #3194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(tools): support additional headers for google api toolset #non-breaking #3194
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @Prhmma, 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 introduces a new feature that allows users to specify optional per-request HTTP headers for Google API toolsets and individual Google API tools. This enhancement provides greater flexibility for integrating with APIs that require custom headers for authentication, tracing, or other specific functionalities. The changes involve modifying the constructors of relevant classes to accept these headers, storing them, and ensuring they are correctly applied to all outgoing API requests, with careful consideration for merging logic to prevent unintended overwrites. Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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 effectively introduces support for additional headers in the Google API toolset, which is a valuable feature enhancement. The implementation is solid, with appropriate changes across the tool and toolset classes, and the logic for merging headers using setdefault
correctly prioritizes more specific headers over the new default ones. The accompanying tests are thorough and cover the new functionality well. I have a few minor suggestions regarding type hint consistency to improve maintainability, but overall, this is a great contribution.
src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py
Outdated
Show resolved
Hide resolved
@seanzhou1023 could you help with the review? |
Response from ADK Triaging Agent Hello @Prhmma, thank you for your contribution! To help us review your PR, could you please add a Also, it looks like some of the automated checks are failing ( You can find more information about our contribution guidelines here: https://github.com/google/adk-python/blob/main/CONTRIBUTING.md Thank you! |
7aa5dde
to
d0ea308
Compare
1c4c464
to
78e4d81
Compare
else operation | ||
) | ||
self.auth_credential, self.auth_scheme = None, None | ||
self._default_headers: Dict[str, str] = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are defining it as a private data member, probably better to move it after line 136 (the Private properties section)?
|
||
def set_default_headers(self, headers: Dict[str, str]): | ||
"""Sets default headers that are merged into every request.""" | ||
self._default_headers = dict(headers) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this dict
cast here?
is_long_running=rest_api_tool.is_long_running, | ||
) | ||
self._rest_api_tool = rest_api_tool | ||
self._rest_api_tool.set_default_headers(additional_headers or {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be if additional_headers: self._rest_api_tool.set_default_headers(additional_headers)
works better? To avoid accidentally clean up default_headers when user not set additional_headers
in GoogleApiTool
.
self._client_id = client_id | ||
self._client_secret = client_secret | ||
self._service_account = service_account | ||
self._additional_headers = dict(additional_headers or {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just do self._additional_headers = additional_headers
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, thanks, I had to change the the type mid way, and I forgot that I already changed it to dict.
@Prhmma Thank you so much for creating this PR! I left some minor comments, please kindly let me know if they sound good to you! |
Thanks @xuanyang15 |
Allow Google API toolsets to accept optional per-request headers
#3105
Testing Plan
Unit Tests
test_init_with_additional_headers
intest_google_api_tool.py
to verify headers are passed to RestApiTooltest_prepare_request_params_merges_default_headers
intest_rest_api_tool.py
to verify custom headers are merged into requeststest_prepare_request_params_preserves_existing_headers
intest_rest_api_tool.py
to verify critical headers (Content-Type, User-Agent) are not overridden by additional_headerstest_init
andtest_get_tools
intest_google_api_toolset.py
to verify the parameter is properly stored and passed throughManual Testing
Tested with Google Ads API scenario (the original use case from issue #3105):