{"record":{"id":"a249ebd0fe152a91","repo":"BerriAI/litellm","slug":"openai-client-is-not-an-instance-of-asyncopenai-m-a249eb","errorCode":null,"errorMessage":"OpenAI client is not an instance of AsyncOpenAI. Make sure you passed an AsyncOpenAI client.","messagePattern":"OpenAI client is not an instance of AsyncOpenAI\\. Make sure you passed an AsyncOpenAI client\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/openai/fine_tuning/handler.py","lineNumber":132,"sourceCode":"    ) -> LiteLLMFineTuningJob | Coroutine[Any, Any, LiteLLMFineTuningJob]:\n        openai_client: Final[OpenAI | AsyncOpenAI | AzureOpenAI | AsyncAzureOpenAI | None] = self.get_openai_client(\n            api_key=api_key,\n            api_base=api_base,\n            timeout=timeout,\n            max_retries=max_retries,\n            organization=organization,\n            client=client,\n            _is_async=_is_async,\n            api_version=api_version,\n        )\n        if openai_client is None:\n            raise ValueError(\n                \"OpenAI client is not initialized. Make sure api_key is passed or OPENAI_API_KEY is set in the environment.\"\n            )\n\n        if _is_async is True:\n            if not isinstance(openai_client, (AsyncOpenAI, AsyncAzureOpenAI)):\n                raise ValueError(\n                    \"OpenAI client is not an instance of AsyncOpenAI. Make sure you passed an AsyncOpenAI client.\"\n                )\n            return self.acreate_fine_tuning_job(\n                create_fine_tuning_job_data=create_fine_tuning_job_data,\n                openai_client=openai_client,\n            )\n        verbose_logger.debug(\"creating fine tuning job, args= %s\", create_fine_tuning_job_data)\n        response: Final = cast(OpenAI, openai_client).fine_tuning.jobs.create(**create_fine_tuning_job_data)\n        return _litellm_fine_tuning_job_from_response(response)\n\n    async def acancel_fine_tuning_job(\n        self,\n        fine_tuning_job_id: str,\n        openai_client: AsyncOpenAI | AsyncAzureOpenAI,\n    ) -> LiteLLMFineTuningJob:\n        response: Final = await openai_client.fine_tuning.jobs.cancel(fine_tuning_job_id=fine_tuning_job_id)\n        return _litellm_fine_tuning_job_from_response(response)\n","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/openai/fine_tuning/handler.py#L114-L150","documentation":"When litellm's fine-tuning job creation is invoked in async mode (_is_async=True, i.e. via acreate_fine_tuning_job paths) it validates that the resolved client is an AsyncOpenAI or AsyncAzureOpenAI instance. Passing a synchronous openai.OpenAI client (or any other object) raises this ValueError because sync SDK clients cannot be awaited.","triggerScenarios":"Calling the async create fine-tuning flow while supplying client=openai.OpenAI(...) (sync), or a custom object masquerading as a client. Also occurs when code built the client with _is_async semantics mismatched - e.g. a shared helper that always constructs sync OpenAI clients being reused for the async call.","commonSituations":"Refactoring sync scripts to asyncio and reusing the old client=OpenAI(...) injection; dependency-injection containers providing one global sync client; passing an AzureOpenAI (sync) variant to async code paths.","solutions":["For async flows, pass client=AsyncOpenAI(api_key=...) (or AsyncAzureOpenAI for Azure) instead of the sync class","Or omit client= entirely and pass api_key so litellm constructs the correct async client itself (_is_async drives the choice)","Keep separate sync and async client instances in your DI setup and select based on the calling context"],"exampleFix":"# before\nsync_client = openai.OpenAI(api_key=KEY)\njob = await handler.acreate_fine_tuning_job_wrapper(..., client=sync_client)  # ValueError\n\n# after\nasync_client = openai.AsyncOpenAI(api_key=KEY)\njob = await handler.acreate_fine_tuning_job_wrapper(..., client=async_client)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"import openai\n\ndef is_async_client(client) -> bool:\n    \"\"\"True when the client can serve litellm async fine-tuning calls.\"\"\"\n    return isinstance(client, (openai.AsyncOpenAI, openai.AsyncAzureOpenAI))\n\n# guard before the call\nassert is_async_client(my_client), \"async fine-tuning needs AsyncOpenAI/AsyncAzureOpenAI\"","tryCatchPattern":"try:\n    job = await handler.acreate_fine_tuning_job(..., client=my_client)\nexcept ValueError as e:\n    if \"not an instance of AsyncOpenAI\" in str(e):\n        my_client = openai.AsyncOpenAI(api_key=KEY)  # rebuild correctly and retry\n        job = await handler.acreate_fine_tuning_job(..., client=my_client)\n    else:\n        raise","preventionTips":["Name client variables by mode (openai_sync vs openai_async) so the wrong one is obvious at call sites","Have your client factory take an is_async flag and return the matching class","Omit client= and pass api_key instead - litellm then constructs the right client type for the call mode","Add a unit test asserting the injected client type for every async code path"],"tags":["fine-tuning","async","client-init","type-mismatch","openai"],"backgroundTag":"sync-async-client-mismatch","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}