{"record":{"id":"f4841cb6b22824f4","repo":"BerriAI/litellm","slug":"invalid-authorization-header-format-expected-bea","errorCode":null,"errorMessage":"Invalid Authorization header format. Expected: Bearer <token>","messagePattern":"Invalid Authorization header format\\. Expected: Bearer <token>","errorType":"http","errorClass":"HTTPException","httpStatus":401,"severity":"error","filePath":"cookbook/mock_guardrail_server/mock_bedrock_guardrail_server.py","lineNumber":187,"sourceCode":"\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\n    if token != GUARDRAIL_CONFIG.bearer_token:\n        raise HTTPException(\n            status_code=status.HTTP_403_FORBIDDEN,\n            detail=\"Invalid bearer token\",\n        )\n\n    return token\n\n\n# ============================================================================","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/cookbook/mock_guardrail_server/mock_bedrock_guardrail_server.py#L169-L205","documentation":"A type guard in async fine-tuning job creation: when _is_async=True, the resolved client must be an AsyncOpenAI (or AsyncAzureOpenAI). If a synchronous OpenAI/AzureOpenAI client was supplied (typical when a user passes client=OpenAI(...) but calls the async entrypoint, or when get_openai_client defaulted to a sync client), this ValueError prevents an awaited call on a non-awaitable. It is a client-type mismatch error raised locally.","triggerScenarios":"Calling the async fine-tuning path (e.g. await litellm.acreate_fine_tuning_job(...) or _is_async=True) while passing a synchronous OpenAI() client, or constructing the handler in a way that resolves to a sync client for an async operation.","commonSituations":"Porting sync example code to asyncio and forgetting to swap OpenAI() for AsyncOpenAI(); shared client factories returning sync clients for both paths; copy-pasting sync client setup into FastAPI async endpoints.","solutions":["Pass an AsyncOpenAI client: client = AsyncOpenAI(api_key=...) when using async fine-tuning.","Or drop the custom client and let litellm build the async client from OPENAI_API_KEY.","Or use the synchronous create_fine_tuning_job() if you cannot switch clients."],"exampleFix":"# before\nclient = OpenAI(api_key=key)\njob = await litellm.acreate_fine_tuning_job(..., client=client)  # raises\n\n# after\nclient = AsyncOpenAI(api_key=key)\njob = await litellm.acreate_fine_tuning_job(..., client=client)","handlingStrategy":"type-guard","validationCode":"from openai import AsyncOpenAI\n\ndef is_async_client(client: object) -> bool:\n    return isinstance(client, AsyncOpenAI)","typeGuard":"from openai import AsyncOpenAI, AsyncAzureOpenAI\n\ndef is_usable_async_client(client: object) -> bool:\n    return isinstance(client, (AsyncOpenAI, AsyncAzureOpenAI))","tryCatchPattern":"try:\n    job = await litellm.acreate_fine_tuning_job(..., client=client)\nexcept ValueError as e:\n    if \"AsyncOpenAI\" in str(e):\n        from openai import AsyncOpenAI\n        job = await litellm.acreate_fine_tuning_job(..., client=AsyncOpenAI(api_key=key))\n    else:\n        raise","preventionTips":["Build clients in one factory with an is_async flag so sync/async paths never mix.","Name variables async_client / sync_client explicitly to catch mismatches at review."],"tags":["openai","fine-tuning","async","type-mismatch","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}