datawhalechina/hello-agents · error · ValueError

MX_APIKEY 环境变量未设置,请先设置环境变量: export MX_APIKEY=your_api_key_he

Error message

MX_APIKEY 环境变量未设置,请先设置环境变量:
export MX_APIKEY=your_api_key_here
或者在初始化时传入 api_key 参数

What it means

MXData.__init__ raises ValueError when both the api_key parameter and the MX_APIKEY environment variable are empty. It is an identical credential guard to the one in mx_search.py, applied to the financial-data query client (https://mkapi2.dfcfs.com/finskillshub/api/claw/query). Because the check runs in the constructor, any downstream MXData().query(...) call can never execute until the key is supplied.

Source

Thrown at Co-creation-projects/lcyting-StockSage-agent/skills/金融数据/mx-data/mx_data.py:194

                    wide_rows.append(row_d)
                return wide_rows, col_labels

    # Fallback
    return [], []

class MXData:
    """妙想金融数据查询客户端"""
    
    BASE_URL = "https://mkapi2.dfcfs.com/finskillshub/api/claw/query"
    
    def __init__(self, api_key: Optional[str] = None):
        """
        初始化客户端
        :param api_key: MX API Key,如果不提供则从环境变量 MX_APIKEY 读取
        """
        self.api_key = api_key or os.getenv("MX_APIKEY")
        if not self.api_key:
            raise ValueError(
                "MX_APIKEY 环境变量未设置,请先设置环境变量:\n"
                "export MX_APIKEY=your_api_key_here\n"
                "或者在初始化时传入 api_key 参数"
            )
    
    def query(self, tool_query: str) -> Dict[str, Any]:
        """
        查询金融数据
        :param tool_query: 自然语言查询问句,如 "东方财富最新价"
        :return: API 响应结果
        """
        headers = {
            "Content-Type": "application/json",
            "apikey": self.api_key
        }
        data = {
            "toolQuery": tool_query
        }

View on GitHub (pinned to 606a07d341)

Solutions

  1. Construct with the key: MXData(api_key=os.environ["MX_APIKEY"]).
  2. export MX_APIKEY=your_api_key_here in the shell that launches the script and verify with print(os.getenv("MX_APIKEY")).
  3. Call load_dotenv() at program entry if the key is stored in .env next to the skill.
  4. Inject MX_APIKEY into the deployment environment (Docker ENV/secret, systemd Environment=, CI secret variable).
  5. Centralize the check: one shared get_mx_apikey() helper used by both MXSearch and MXData so the failure message is consistent.

Example fix

// before
data = MXData()
// after
import os
api_key = os.getenv("MX_APIKEY")
if not api_key:
    raise SystemExit("MX_APIKEY missing — add it to .env and run load_dotenv()")
data = MXData(api_key=api_key)
Defensive patterns

Strategy: validation

Validate before calling

import os
if not os.getenv("MX_APIKEY"):
    raise SystemExit("MX_APIKEY not set — add it to the environment or .env before using MXData")

Try / catch

try:
    data = MXData(api_key=os.getenv("MX_APIKEY"))
except ValueError:
    logger.error("MX_APIKEY missing; refusing to start")
    raise

Prevention

When it happens

Trigger: Instantiating MXData() without arguments in a process where MX_APIKEY is unset; passing api_key="" or None explicitly; setting the var after the Python process started (os.getenv only reads at call time inside __init__, so late exports in the parent shell don't help a running interpreter); CI jobs and Docker containers that were not given the variable.

Common situations: Following the skill README on a new machine without completing the env setup step; secrets managed in .env but the data skill script has no load_dotenv(); key configured for the search skill (mx_search) but the data skill run by a different launcher lacking the env; name mismatch such as MX_API_KEY.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/5f75a7860f255195. Report an issue: GitHub.