assafelovic/gpt-researcher · error · ValueError

Cannot convert {env_value} to dict

Error message

Cannot convert {env_value} to dict

What it means

For dict-typed config attributes, convert_env_value parses the env string with json_repair; if parsing raises entirely, this ValueError is raised chained from the exception.

Source

Thrown at gpt_researcher/config/config.py:301

        elif type_hint is float:
            return float(env_value)
        elif type_hint in (str, Any):
            return env_value
        elif type_hint is list or origin is list or origin is List:
            # Env values are often hand-edited (trailing commas, single quotes).
            # Bare `list` has get_origin(None); typing.List[...] has origin list.
            try:
                value = json_repair.loads(env_value)
            except Exception as exc:
                raise ValueError(f"Cannot convert {env_value} to list") from exc
            if not isinstance(value, list):
                raise ValueError(f"Cannot convert {env_value} to list")
            return value
        elif type_hint is dict:
            try:
                value = json_repair.loads(env_value)
            except Exception as exc:
                raise ValueError(f"Cannot convert {env_value} to dict") from exc
            if not isinstance(value, dict):
                raise ValueError(f"Cannot convert {env_value} to dict")
            return value
        else:
            raise ValueError(f"Unsupported type {type_hint} for key {key}")


    def set_verbose(self, verbose: bool) -> None:
        """Set the verbosity level."""
        self.llm_kwargs["verbose"] = verbose

    def get_mcp_server_config(self, name: str) -> dict:
        """
        Get the configuration for an MCP server.
        
        Args:
            name (str): The name of the MCP server to get the config for.
                

View on GitHub (pinned to 6f998577d5)

Solutions

  1. Use strict JSON object syntax: MYDICT='{"k":"v"}'
  2. Quote inner strings; wrap the whole value in single quotes in .env so the shell keeps double quotes

Example fix

# before
MYDICT={k: v}
# after
MYDICT='{"k": "v"}'
Defensive patterns

Strategy: validation

Validate before calling

import json
v = os.getenv("MY_DICT", "{}")
json.loads(v)  # raises early with clear context if unparseable

Type guard

def is_json_dict(s):
    try:
        return isinstance(json_repair.loads(s), dict)
    except Exception:
        return False

Try / catch

try:
    cfg = Config()
except ValueError as e:
    if "to dict" in str(e):
        print('Fix MY_DICT to JSON object syntax'); exit(2)
    raise

Prevention

When it happens

Trigger: A dict-typed env var with unparseable content, e.g. MYDICT='key: value' (not JSON) where json_repair gives up.

Common situations: YAML-style or Python-dict literals in env vars; missing braces; free text on a dict field.

Related errors


AI-assisted analysis of assafelovic/gpt-researcher@6f998577d5 (2026-08-28). Data as JSON: /api/errors/4fb24dded9975453. Report an issue: GitHub.