pip install litellm★SDK only. Addlitellm[proxy]for the gateway server.from litellm import completion r = completion(model="openai/gpt-4o", messages=msgs)★The universal call. Swap the model string to change provider — everything else stays identical.r.choices[0].message.content★Response follows the OpenAI schema for every provider:choices,message,usage,id,model.export OPENAI_API_KEY=... ANTHROPIC_API_KEY=... GEMINI_API_KEY=...Keys come from env vars by default (per provider), or passapi_key=explicitly.
msgs = [{"role":"system","content":"..."}, {"role":"user","content":"Hi"}]★Standard chat-message list. Multimodal content parts (image/audio) supported for capable models.completion(model, messages, temperature=0.7, max_tokens=500)★OpenAI-style params (top_p,stop,n,seed) are translated to each provider's native format.completion(..., response_format={"type":"json_object"})JSON mode / structured output; pass a Pydantic model asresponse_format=where supported.completion(..., tools=tools, tool_choice="auto")Function/tool calling with the OpenAI schema; readr.choices[0].message.tool_calls.
for chunk in completion(model, messages, stream=True): print(chunk.choices[0].delta.content or "", end="")★Streaming yields OpenAI-styledeltachunks for every provider.from litellm import acompletion r = await acompletion(model, messages)★Async twin ofcompletion.stream=Truegives an async iterator.from litellm import stream_chunk_builder full = stream_chunk_builder(chunks, messages=msgs)Reassemble streamed chunks into a single completion object (with usage).
from litellm import embedding embedding(model="openai/text-embedding-3-small", input=["txt"])★Same unified shape for embeddings.aembeddingfor async.litellm.transcription(...) · image_generation(...) · rerank(...)Audio, image and rerank endpoints where the provider supports them.litellm.completion(model="...", mock_response="hi")Return a canned response — test your app without hitting (or paying) a provider.