{"record":{"id":"f93e3b8ce6a70438","repo":"assafelovic/gpt-researcher","slug":"cannot-convert-env-value-to-any-of-args","errorCode":null,"errorMessage":"Cannot convert {env_value} to any of {args}","messagePattern":"Cannot convert (.+?) to any of (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"gpt_researcher/config/config.py","lineNumber":277,"sourceCode":"        \"\"\"Convert environment variable to the appropriate type based on the type hint.\"\"\"\n        origin = get_origin(type_hint)\n        args = get_args(type_hint)\n\n        if origin is Union:\n            # Handle Union types (e.g., Union[str, None] / Optional[str]).\n            # Check the None sentinel BEFORE non-None args: for Optional[str],\n            # str conversion never raises, so looping str-first permanently\n            # shadowed the none/null/\"\" → None branch (see issue #1899).\n            if type(None) in args and env_value.lower() in (\"none\", \"null\", \"\"):\n                return None\n            for arg in args:\n                if arg is type(None):\n                    continue\n                try:\n                    return Config.convert_env_value(key, env_value, arg)\n                except ValueError:\n                    continue\n            raise ValueError(f\"Cannot convert {env_value} to any of {args}\")\n\n        if type_hint is bool:\n            return env_value.lower() in (\"true\", \"1\", \"yes\", \"on\")\n        elif type_hint is int:\n            return int(env_value)\n        elif type_hint is float:\n            return float(env_value)\n        elif type_hint in (str, Any):\n            return env_value\n        elif type_hint is list or origin is list or origin is List:\n            # Env values are often hand-edited (trailing commas, single quotes).\n            # Bare `list` has get_origin(None); typing.List[...] has origin list.\n            try:\n                value = json_repair.loads(env_value)\n            except Exception as exc:\n                raise ValueError(f\"Cannot convert {env_value} to list\") from exc\n            if not isinstance(value, list):\n                raise ValueError(f\"Cannot convert {env_value} to list\")","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/assafelovic/gpt-researcher/blob/6f998577d547b1e54ec662dac63583aa11e3b84b/gpt_researcher/config/config.py#L259-L295","documentation":"Config.convert_env_value tries each type in an Optional/Union type hint in turn; when no branch can convert the raw env string, it raises this aggregated ValueError. It is the fallback for Union types where every candidate conversion failed.","triggerScenarios":"An env var annotated Optional[int] set to 'abc', or Optional[bool] set to 'maybe'—each inner conversion raises and the loop exhausts, producing this message.","commonSituations":"Hand-edited .env files with non-numeric values for numeric settings; booleans set to 'yeah'; JSON-ish values on scalar fields.","solutions":["Set the env var to a valid literal for the annotated type (true/false for bool, digits for int/float)","Remove the variable to use the default","Check the Config attribute type hints for the key named in the message"],"exampleFix":"# before\nFAST_TOKEN_LIMIT=unlimited\n# after\nFAST_TOKEN_LIMIT=4000","handlingStrategy":"validation","validationCode":"def coerce(env_val, types):\n    for t in types:\n        try:\n            return Config.convert_env_value(\"k\", env_val, t)\n        except ValueError:\n            pass\n    raise ValueError(env_val)\n# preflight any Optional[int] var:\ncoerce(os.getenv('FAST_TOKEN_LIMIT','4000'), (int, type(None)))","typeGuard":null,"tryCatchPattern":"try:\n    cfg = Config()\nexcept ValueError as e:\n    if \"Cannot convert\" in str(e):\n        print('Bad env value:', e); exit(2)\n    raise","preventionTips":["Keep .env values strictly typed literals","Validate env file in CI with a Config() construction test"],"tags":["python","config","env-vars","type-coercion"],"backgroundTag":"env-var-type-conversion-failed","analyzedSha":"6f998577d547b1e54ec662dac63583aa11e3b84b","analyzedAt":"2026-08-28T17:50:07.383Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}