{"record":{"id":"1bebc815a2686cf8","repo":"BerriAI/litellm","slug":"missing-authorization-header","errorCode":null,"errorMessage":"Missing Authorization header","messagePattern":"Missing Authorization header","errorType":"http","errorClass":"HTTPException","httpStatus":401,"severity":"critical","filePath":"cookbook/mock_guardrail_server/mock_bedrock_guardrail_server.py","lineNumber":177,"sourceCode":"# Authentication\n# ============================================================================\n\n\nasync def verify_bearer_token(authorization: Optional[str] = Header(None)) -> str:\n    \"\"\"\n    Verify the Bearer token from the Authorization header.\n\n    Args:\n        authorization: The Authorization header value\n\n    Returns:\n        The token if valid\n\n    Raises:\n        HTTPException: If token is missing or invalid\n    \"\"\"\n    if authorization is None:\n        raise HTTPException(\n            status_code=status.HTTP_401_UNAUTHORIZED,\n            detail=\"Missing Authorization header\",\n            headers={\"WWW-Authenticate\": \"Bearer\"},\n        )\n\n    # Check if it's a Bearer token\n    parts = authorization.split()\n    print(f\"parts: {parts}\")\n    if len(parts) != 2 or parts[0].lower() != \"bearer\":\n        raise HTTPException(\n            status_code=status.HTTP_401_UNAUTHORIZED,\n            detail=\"Invalid Authorization header format. Expected: Bearer <token>\",\n            headers={\"WWW-Authenticate\": \"Bearer\"},\n        )\n\n    token = parts[1]\n\n    # Verify token","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/cookbook/mock_guardrail_server/mock_bedrock_guardrail_server.py#L159-L195","documentation":"A pre-flight guard in OpenAI fine-tuning job creation: get_openai_client() returns None when no usable client can be constructed (no api_key passed, no OPENAI_API_KEY env var, no custom client, and no default api_base that permits an anonymous client). When it returns None, this ValueError stops the call before touching the network. It is purely a local configuration error.","triggerScenarios":"Calling litellm.create_fine_tuning_job() without api_key, without a custom OpenAI client, and without OPENAI_API_KEY set in the environment (or via the secret manager).","commonSituations":"Scripts that previously relied on a globally set key in an interactive shell but run in fresh CI containers; notebooks where os.environ was set after import; Azure workflows that pass azure creds but no OpenAI key while the default OpenAI endpoint is still targeted.","solutions":["Set OPENAI_API_KEY in the environment before running the script.","Or pass api_key=... directly to create_fine_tuning_job.","Or pass a prebuilt OpenAI(...) client via the client= argument.","For Azure, ensure api_base/api_version route to an Azure client so the OpenAI-key requirement is replaced correctly."],"exampleFix":"# before\njob = litellm.create_fine_tuning_job(model=\"gpt-4o-mini-2024-07-18\", training_file=\"file-abc\")\n\n# after\njob = litellm.create_fine_tuning_job(\n    model=\"gpt-4o-mini-2024-07-18\",\n    training_file=\"file-abc\",\n    api_key=os.environ[\"OPENAI_API_KEY\"],\n)","handlingStrategy":"validation","validationCode":"import os\n\ndef has_openai_credentials(api_key: str | None, client: object | None) -> bool:\n    return bool(api_key or client or os.environ.get(\"OPENAI_API_KEY\"))\n\nif not has_openai_credentials(api_key, client):\n    raise SystemExit(\"OPENAI_API_KEY missing; refusing to start fine-tuning\")","typeGuard":null,"tryCatchPattern":"try:\n    job = litellm.create_fine_tuning_job(model=m, training_file=f)\nexcept ValueError as e:\n    if \"not initialized\" in str(e):\n        job = litellm.create_fine_tuning_job(model=m, training_file=f, api_key=os.environ[\"OPENAI_API_KEY\"])\n    else:\n        raise","preventionTips":["Centralize credential resolution in one helper used by all fine-tuning calls.","Set keys in the scheduler/cron environment, not just your interactive shell.","Pass api_key explicitly in orchestration code rather than relying on ambient state."],"tags":["openai","fine-tuning","authentication","configuration","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}