-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(dashscope): support to load headers from the environment #1096
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?
Conversation
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.
Pull request overview
This pull request adds support for loading HTTP headers from the DASHSCOPE_API_HEADERS environment variable for DashScope API calls. This allows users to configure custom headers without modifying code.
Key Changes
- Added environment variable loading for
DASHSCOPE_API_HEADERSin theDashScopeChatModelinitialization - Headers from the environment variable are parsed as JSON and merged with explicitly provided headers in
generate_kwargs - Error handling ensures invalid JSON triggers a warning rather than failing initialization
| import json | ||
| import os |
Copilot
AI
Jan 8, 2026
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.
The json and os imports are being added at the top of the file, which violates the lazy loading guideline. These imports should be moved to the point of use within the __init__ method where they are actually needed. This helps minimize initial import overhead for the module.
| # Load headers from environment variable if exists | ||
| headers = os.getenv("DASHSCOPE_API_HEADERS") | ||
| if headers: | ||
| try: | ||
| headers = json.loads(str(headers)) | ||
| if not isinstance(headers, dict): | ||
| raise json.JSONDecodeError("", "", 0) | ||
|
|
||
| if self.generate_kwargs.get("headers"): | ||
| headers.update(self.generate_kwargs["headers"]) | ||
|
|
||
| self.generate_kwargs["headers"] = headers | ||
|
|
||
| except json.JSONDecodeError: | ||
| logger.warning( | ||
| "Failed to parse DASHSCOPE_API_HEADERS environment " | ||
| "variable as JSON. It should be a JSON object.", | ||
| ) |
Copilot
AI
Jan 8, 2026
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.
The new feature for loading headers from the DASHSCOPE_API_HEADERS environment variable lacks test coverage. Since this file has comprehensive test coverage in tests/model_dashscope_test.py, tests should be added to verify:
- Headers are correctly loaded and parsed from the environment variable
- Invalid JSON in the environment variable triggers a warning
- Headers from
generate_kwargstake precedence over environment headers - The feature works correctly when the environment variable is not set
| if not isinstance(headers, dict): | ||
| raise json.JSONDecodeError("", "", 0) | ||
|
|
||
| if self.generate_kwargs.get("headers"): | ||
| headers.update(self.generate_kwargs["headers"]) | ||
|
|
||
| self.generate_kwargs["headers"] = headers | ||
|
|
||
| except json.JSONDecodeError: | ||
| logger.warning( | ||
| "Failed to parse DASHSCOPE_API_HEADERS environment " | ||
| "variable as JSON. It should be a JSON object.", | ||
| ) | ||
|
|
Copilot
AI
Jan 8, 2026
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.
The error handling approach is unconventional. When the parsed headers are not a dictionary (line 110), the code raises a JSONDecodeError with empty arguments to trigger the exception handler. This is misleading because the JSON parsing actually succeeded - it's the type validation that failed. Consider using a TypeError or ValueError instead, or check the type before attempting JSON parsing and skip the environment variable with a warning if it's not a dictionary.
| if not isinstance(headers, dict): | |
| raise json.JSONDecodeError("", "", 0) | |
| if self.generate_kwargs.get("headers"): | |
| headers.update(self.generate_kwargs["headers"]) | |
| self.generate_kwargs["headers"] = headers | |
| except json.JSONDecodeError: | |
| logger.warning( | |
| "Failed to parse DASHSCOPE_API_HEADERS environment " | |
| "variable as JSON. It should be a JSON object.", | |
| ) | |
| except json.JSONDecodeError: | |
| logger.warning( | |
| "Failed to parse DASHSCOPE_API_HEADERS environment " | |
| "variable as JSON. It should be a JSON object.", | |
| ) | |
| else: | |
| if not isinstance(headers, dict): | |
| logger.warning( | |
| "DASHSCOPE_API_HEADERS environment variable was " | |
| "parsed successfully but is not a JSON object. " | |
| "It should be a JSON object.", | |
| ) | |
| else: | |
| if self.generate_kwargs.get("headers"): | |
| headers.update(self.generate_kwargs["headers"]) | |
| self.generate_kwargs["headers"] = headers |
| headers = os.getenv("DASHSCOPE_API_HEADERS") | ||
| if headers: | ||
| try: | ||
| headers = json.loads(str(headers)) |
Copilot
AI
Jan 8, 2026
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.
The str() conversion on line 109 is unnecessary. os.getenv() already returns a string or None, and the check on line 107 ensures that headers is truthy (not None or empty string). The str() wrapper adds no value and can be removed: headers = json.loads(headers).
| headers = json.loads(str(headers)) | |
| headers = json.loads(headers) |
AgentScope Version
[The version of AgentScope you are working on, e.g.
import agentscope; print(agentscope.__version__)]Description
[Please describe the background, purpose, changes made, and how to test this PR]
Checklist
Please check the following items before code is ready to be reviewed.
pre-commit run --all-filescommand