Skip to content

Conversation

@DavdGao
Copy link
Member

@DavdGao DavdGao commented Jan 8, 2026

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.

  • Code has been formatted with pre-commit run --all-files command
  • All tests are passing
  • Docstrings are in Google style
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

Copy link
Contributor

Copilot AI left a 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_HEADERS in the DashScopeChatModel initialization
  • 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

Comment on lines +4 to +5
import json
import os
Copy link

Copilot AI Jan 8, 2026

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.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +105 to +122
# 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.",
)
Copy link

Copilot AI Jan 8, 2026

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:

  1. Headers are correctly loaded and parsed from the environment variable
  2. Invalid JSON in the environment variable triggers a warning
  3. Headers from generate_kwargs take precedence over environment headers
  4. The feature works correctly when the environment variable is not set

Copilot uses AI. Check for mistakes.
Comment on lines +110 to +123
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.",
)

Copy link

Copilot AI Jan 8, 2026

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
headers = os.getenv("DASHSCOPE_API_HEADERS")
if headers:
try:
headers = json.loads(str(headers))
Copy link

Copilot AI Jan 8, 2026

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).

Suggested change
headers = json.loads(str(headers))
headers = json.loads(headers)

Copilot uses AI. Check for mistakes.
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.

1 participant