BerriAI/litellm · error · ValueError
OpenAI client is not an instance of AsyncOpenAI. Make sure…
Error message
OpenAI client is not an instance of AsyncOpenAI. Make sure you passed an AsyncOpenAI client.
What it means
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.
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
Example fix
# before sync_client = openai.OpenAI(api_key=KEY) job = await handler.acreate_fine_tuning_job_wrapper(..., client=sync_client) # ValueError # after async_client = openai.AsyncOpenAI(api_key=KEY) job = await handler.acreate_fine_tuning_job_wrapper(..., client=async_client)
Defensive patterns
Strategy: type-guard
Type guard
import openai
def is_async_client(client) -> bool:
"""True when the client can serve litellm async fine-tuning calls."""
return isinstance(client, (openai.AsyncOpenAI, openai.AsyncAzureOpenAI))
# guard before the call
assert is_async_client(my_client), "async fine-tuning needs AsyncOpenAI/AsyncAzureOpenAI" Try / catch
try:
job = await handler.acreate_fine_tuning_job(..., client=my_client)
except ValueError as e:
if "not an instance of AsyncOpenAI" in str(e):
my_client = openai.AsyncOpenAI(api_key=KEY) # rebuild correctly and retry
job = await handler.acreate_fine_tuning_job(..., client=my_client)
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- OpenAI client is not initialized. Make sure api_key is…
- A2A send_message failed: non-streaming message/send expects…
- API base is required for OpenAI image variations
- API key is required for OpenAI image variations
- CANCEL eval is not supported for
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/a249ebd0fe152a91.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/openai/fine_tuning/handler.py:132
) -> LiteLLMFineTuningJob | Coroutine[Any, Any, LiteLLMFineTuningJob]:
openai_client: Final[OpenAI | AsyncOpenAI | AzureOpenAI | AsyncAzureOpenAI | None] = self.get_openai_client(
api_key=api_key,
api_base=api_base,
timeout=timeout,
max_retries=max_retries,
organization=organization,
client=client,
_is_async=_is_async,
api_version=api_version,
)
if openai_client is None:
raise ValueError(
"OpenAI client is not initialized. Make sure api_key is passed or OPENAI_API_KEY is set in the environment."
)
if _is_async is True:
if not isinstance(openai_client, (AsyncOpenAI, AsyncAzureOpenAI)):
raise ValueError(
"OpenAI client is not an instance of AsyncOpenAI. Make sure you passed an AsyncOpenAI client."
)
return self.acreate_fine_tuning_job(
create_fine_tuning_job_data=create_fine_tuning_job_data,
openai_client=openai_client,
)
verbose_logger.debug("creating fine tuning job, args= %s", create_fine_tuning_job_data)
response: Final = cast(OpenAI, openai_client).fine_tuning.jobs.create(**create_fine_tuning_job_data)
return _litellm_fine_tuning_job_from_response(response)
async def acancel_fine_tuning_job(
self,
fine_tuning_job_id: str,
openai_client: AsyncOpenAI | AsyncAzureOpenAI,
) -> LiteLLMFineTuningJob:
response: Final = await openai_client.fine_tuning.jobs.cancel(fine_tuning_job_id=fine_tuning_job_id)
return _litellm_fine_tuning_job_from_response(response)
View on GitHub (pinned to 77b7c6c40c)