AnyLLM
AnyLLM
any_llm.AnyLLM
Bases: ABC
Provider for the LLM.
Source code in src/any_llm/any_llm.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
|
API_BASE = None
class-attribute
instance-attribute
This is used to set the API base for the provider. It is not required but may prove useful for providers that have overridable api bases.
ENV_API_KEY_NAME
instance-attribute
Environment variable name for the API key
MISSING_PACKAGES_ERROR = None
class-attribute
instance-attribute
Some providers use SDKs that are not installed by default. This flag is used to check if the packages are installed before instantiating the provider.
PROVIDER_DOCUMENTATION_URL
instance-attribute
Link to the provider's documentation
PROVIDER_NAME
instance-attribute
Must match the name of the provider directory (case sensitive)
SUPPORTS_COMPLETION
instance-attribute
OpenAI Completion API
SUPPORTS_COMPLETION_IMAGE
instance-attribute
Image Support for Completion API
SUPPORTS_COMPLETION_PDF
instance-attribute
PDF Support for Completion API
SUPPORTS_COMPLETION_REASONING
instance-attribute
Reasoning Content attached to Completion API Response
SUPPORTS_COMPLETION_STREAMING
instance-attribute
OpenAI Streaming Completion API
SUPPORTS_EMBEDDING
instance-attribute
OpenAI Embedding API
SUPPORTS_LIST_MODELS
instance-attribute
OpenAI Models API
SUPPORTS_RESPONSES
instance-attribute
OpenAI Responses API
acompletion(model, messages, *, tools=None, tool_choice=None, temperature=None, top_p=None, max_tokens=None, response_format=None, stream=None, n=None, stop=None, presence_penalty=None, frequency_penalty=None, seed=None, user=None, parallel_tool_calls=None, logprobs=None, top_logprobs=None, logit_bias=None, stream_options=None, max_completion_tokens=None, reasoning_effort='auto', **kwargs)
async
Create a chat completion asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model identifier for the chosen provider (e.g., model='gpt-4.1-mini' for LLMProvider.OPENAI). |
required |
messages
|
list[dict[str, Any] | ChatCompletionMessage]
|
List of messages for the conversation |
required |
tools
|
list[dict[str, Any] | Callable[..., Any]] | None
|
List of tools for tool calling. Can be Python callables or OpenAI tool format dicts |
None
|
tool_choice
|
str | dict[str, Any] | None
|
Controls which tools the model can call |
None
|
temperature
|
float | None
|
Controls randomness in the response (0.0 to 2.0) |
None
|
top_p
|
float | None
|
Controls diversity via nucleus sampling (0.0 to 1.0) |
None
|
max_tokens
|
int | None
|
Maximum number of tokens to generate |
None
|
response_format
|
dict[str, Any] | type[BaseModel] | None
|
Format specification for the response |
None
|
stream
|
bool | None
|
Whether to stream the response |
None
|
n
|
int | None
|
Number of completions to generate |
None
|
stop
|
str | list[str] | None
|
Stop sequences for generation |
None
|
presence_penalty
|
float | None
|
Penalize new tokens based on presence in text |
None
|
frequency_penalty
|
float | None
|
Penalize new tokens based on frequency in text |
None
|
seed
|
int | None
|
Random seed for reproducible results |
None
|
user
|
str | None
|
Unique identifier for the end user |
None
|
parallel_tool_calls
|
bool | None
|
Whether to allow parallel tool calls |
None
|
logprobs
|
bool | None
|
Include token-level log probabilities in the response |
None
|
top_logprobs
|
int | None
|
Number of alternatives to return when logprobs are requested |
None
|
logit_bias
|
dict[str, float] | None
|
Bias the likelihood of specified tokens during generation |
None
|
stream_options
|
dict[str, Any] | None
|
Additional options controlling streaming behavior |
None
|
max_completion_tokens
|
int | None
|
Maximum number of tokens for the completion |
None
|
reasoning_effort
|
Literal['minimal', 'low', 'medium', 'high', 'auto'] | None
|
Reasoning effort level for models that support it. "auto" will map to each provider's default. |
'auto'
|
**kwargs
|
Any
|
Additional provider-specific arguments that will be passed to the provider's API call. |
{}
|
Returns:
Type | Description |
---|---|
ChatCompletion | AsyncIterator[ChatCompletionChunk]
|
The completion response from the provider |
Source code in src/any_llm/any_llm.py
aresponses(model, input_data, *, tools=None, tool_choice=None, max_output_tokens=None, temperature=None, top_p=None, stream=None, instructions=None, max_tool_calls=None, parallel_tool_calls=None, reasoning=None, text=None, **kwargs)
async
Create a response using the OpenAI-style Responses API.
This follows the OpenAI Responses API shape and returns the aliased
any_llm.types.responses.Response
type. If stream=True
, an iterator of
any_llm.types.responses.ResponseStreamEvent
items is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model identifier for the chosen provider (e.g., model='gpt-4.1-mini' for LLMProvider.OPENAI). |
required |
input_data
|
str | ResponseInputParam
|
The input payload accepted by provider's Responses API. For OpenAI-compatible providers, this is typically a list mixing text, images, and tool instructions, or a dict per OpenAI spec. |
required |
tools
|
list[dict[str, Any] | Callable[..., Any]] | None
|
Optional tools for tool calling (Python callables or OpenAI tool dicts) |
None
|
tool_choice
|
str | dict[str, Any] | None
|
Controls which tools the model can call |
None
|
max_output_tokens
|
int | None
|
Maximum number of output tokens to generate |
None
|
temperature
|
float | None
|
Controls randomness in the response (0.0 to 2.0) |
None
|
top_p
|
float | None
|
Controls diversity via nucleus sampling (0.0 to 1.0) |
None
|
stream
|
bool | None
|
Whether to stream response events |
None
|
instructions
|
str | None
|
A system (or developer) message inserted into the model's context. |
None
|
max_tool_calls
|
int | None
|
The maximum number of total calls to built-in tools that can be processed in a response. This maximum number applies across all built-in tool calls, not per individual tool. Any further attempts to call a tool by the model will be ignored. |
None
|
parallel_tool_calls
|
int | None
|
Whether to allow the model to run tool calls in parallel. |
None
|
reasoning
|
Any | None
|
Configuration options for reasoning models. |
None
|
text
|
Any | None
|
Configuration options for a text response from the model. Can be plain text or structured JSON data. |
None
|
**kwargs
|
Any
|
Additional provider-specific arguments that will be passed to the provider's API call. |
{}
|
Returns:
Type | Description |
---|---|
Response | AsyncIterator[ResponseStreamEvent]
|
Either a |
Response | AsyncIterator[ResponseStreamEvent]
|
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the selected provider does not support the Responses API. |
Source code in src/any_llm/any_llm.py
completion(**kwargs)
Create a chat completion synchronously.
Source code in src/any_llm/any_llm.py
create(provider, api_key=None, api_base=None, **kwargs)
classmethod
Create a provider instance using the given provider name and config.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
provider
|
str | LLMProvider
|
The provider name (e.g., 'openai', 'anthropic') |
required |
api_key
|
str | None
|
API key for the provider |
None
|
api_base
|
str | None
|
Base URL for the provider API |
None
|
**kwargs
|
Any
|
Additional provider-specific arguments |
{}
|
Returns:
Type | Description |
---|---|
AnyLLM
|
Provider instance for the specified provider |
Source code in src/any_llm/any_llm.py
get_all_provider_metadata()
classmethod
Get metadata for all supported providers.
Returns:
Type | Description |
---|---|
list[ProviderMetadata]
|
List of dictionaries containing provider metadata |
Source code in src/any_llm/any_llm.py
get_provider_class(provider_key)
classmethod
Get the provider class without instantiating it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
provider_key
|
str | LLMProvider
|
The provider key (e.g., 'anthropic', 'openai') |
required |
Returns:
Type | Description |
---|---|
type[AnyLLM]
|
The provider class |
Source code in src/any_llm/any_llm.py
get_provider_enum(provider_key)
classmethod
Convert a string provider key to a ProviderName enum.
Source code in src/any_llm/any_llm.py
get_provider_metadata()
classmethod
Get provider metadata without requiring instantiation.
Returns:
Type | Description |
---|---|
ProviderMetadata
|
Dictionary containing provider metadata including name, environment variable, |
ProviderMetadata
|
documentation URL, and class name. |
Source code in src/any_llm/any_llm.py
get_supported_providers()
classmethod
responses(**kwargs)
Create a response synchronously.
Source code in src/any_llm/any_llm.py
split_model_provider(model)
classmethod
Extract the provider key from the model identifier.
Supports both new format 'provider:model' (e.g., 'mistral:mistral-small') and legacy format 'provider/model' (e.g., 'mistral/mistral-small').
The legacy format will be deprecated in version 1.0.