{"record":{"id":"4b391f5079a76f92","repo":"xtekky/gpt4free","slug":"invalid-azure-routes-environment-variable-format","errorCode":null,"errorMessage":"Invalid AZURE_ROUTES environment variable format: {routes}","messagePattern":"Invalid AZURE_ROUTES environment variable format: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Azure.py","lineNumber":49,"sourceCode":"        }\n    }\n    api_keys: dict[str, str] = {}\n    failed: dict[str, int] = {}\n\n    @classmethod\n    def get_models(cls, api_key: str = None, **kwargs) -> list[str]:\n        api_keys = os.environ.get(\"AZURE_API_KEYS\")\n        if api_keys:\n            try:\n                cls.api_keys = json.loads(api_keys)\n            except json.JSONDecodeError:\n                raise ValueError(f\"Invalid AZURE_API_KEYS environment variable\")\n        routes = os.environ.get(\"AZURE_ROUTES\")\n        if routes:\n            try:\n                routes = json.loads(routes)\n            except json.JSONDecodeError:\n                raise ValueError(\n                    f\"Invalid AZURE_ROUTES environment variable format: {routes}\"\n                )\n            cls.routes = routes\n        if cls.routes:\n            if cls.live == 0 and cls.api_keys:\n                cls.live += 1\n            return list(cls.routes.keys())\n        return super().get_models(api_key=api_key, **kwargs)\n\n    @classmethod\n    async def create_async_generator(\n        cls,\n        model: str,\n        messages: Messages,\n        stream: bool = True,\n        media: MediaListType = None,\n        api_key: str = None,\n        api_endpoint: str = None,","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Azure.py#L31-L67","documentation":"AZURE_ROUTES must be a JSON object mapping model names to Azure OpenAI api_endpoint URLs. Raised in get_models() when json.loads() fails on the raw environment value. The offending value is embedded in the message, making the syntax error easy to spot.","triggerScenarios":"Setting AZURE_ROUTES to anything non-JSON (a bare URL, unquoted keys, single-quoted dict) and invoking get_models() or create_async_generator without an explicit api_endpoint.","commonSituations":"Copied a Python-dict-style routes mapping into a .env file; YAML-style config pasted as env; missing closing brace after hand-editing routes.","solutions":["Use strict JSON with double-quoted keys and values: AZURE_ROUTES='{\"gpt-4o\": \"https://res.openai.azure.com/openai/deployments/gpt-4o\"}'.","Run echo \"$AZURE_ROUTES\" | python -m json.tool to pinpoint the syntax error.","Keep routes and keys env vars in one place (.env loaded by the app) so they are validated together."],"exampleFix":"# before\nexport AZURE_ROUTES=\"{'gpt-4o': 'https://res.openai.azure.com/...'}\"\n\n# after\nexport AZURE_ROUTES='{\"gpt-4o\": \"https://res.openai.azure.com/openai/deployments/gpt-4o\"}'","handlingStrategy":"validation","validationCode":"import json, os\n\ndef validate_azure_routes() -> dict:\n    raw = os.environ.get(\"AZURE_ROUTES\")\n    if not raw:\n        return {}\n    parsed = json.loads(raw)  # fail fast at startup with clear error\n    assert isinstance(parsed, dict) and all(isinstance(v, str) for v in parsed.values()), \\\n        \"AZURE_ROUTES must be a JSON object of model -> endpoint URL strings\"\n    return parsed","typeGuard":"def is_valid_routes_env(raw: str | None) -> bool:\n    \"\"\"True when AZURE_ROUTES is absent or a JSON object of URL strings.\"\"\"\n    if not raw:\n        return True\n    try:\n        parsed = json.loads(raw)\n    except json.JSONDecodeError:\n        return False\n    return isinstance(parsed, dict) and all(isinstance(v, str) for v in parsed.values())","tryCatchPattern":"try:\n    models = Azure.get_models()\nexcept ValueError as e:\n    if \"AZURE_ROUTES\" in str(e):\n        # message embeds the bad value: fix JSON syntax, restart process\n        raise","preventionTips":["json.loads-validate AZURE_ROUTES during boot","Never paste Python-dict/YAML syntax into env vars","Keep a tested .env.example with valid JSON routes as the template"],"tags":["configuration","environment","json","azure"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}