Metadata-Version: 2.4
Name: ellm-adapter
Version: 0.3.1
Summary: One call_llm function for Anthropic, OpenAI and Gemini: structured output, per-model parameter rules and a fallback model.
License-Expression: Apache-2.0
Keywords: llm,anthropic,claude,openai,gemini,structured-output,fallback
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: anthropic<3,>=2.3
Requires-Dist: openai<4,>=3.37
Requires-Dist: google-genai<3,>=2.33
Requires-Dist: pydantic>=2
Provides-Extra: dev
Requires-Dist: python-dotenv; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# ellm-adapter

One `call_llm` function for Anthropic, OpenAI and Gemini language models. Switching providers means changing a model name, nothing else.

- **The provider comes from the model name.** `call_llm("claude-sonnet-5-5", ...)` and `call_llm("gpt-6.1-sol", ...)` take the same arguments and return the same `LLMResponse`.
- **Structured output** from a Pydantic class, validated before it is returned.
- **Per-model parameter rules.** `temperature` and `reasoning_effort` are sent only where the model accepts them; anything else is left out and listed in `dropped_params`, so a model switch never breaks a call.
- **A fallback model.** If the first model fails in any way, the same request goes to another one, on any provider.
- **Comparable token counts** across providers: input includes cached input, output includes thinking.

## Install

```bash
pip install ellm-adapter
```

Requires Python 3.10+. Set the API keys of the providers you use: `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, `GEMINI_API_KEY`.

## Usage

```python
from pydantic import BaseModel
from ellm_adapter import call_llm

class Outline(BaseModel):
    title: str
    slides: list[str]

response = call_llm(
    model="claude-sonnet-5-5",
    messages=[{"role": "user", "content": "Write a 5-slide outline on cybersecurity."}],
    system_prompt="You are an instructional content expert.",
    response_schema=Outline,            # optional; without it, use response.text
    reasoning_effort="low",             # optional
    fallback_model="gpt-6.1-sol",       # optional
)

outline = response.parsed               # an Outline instance
print(response.provider, response.model, response.input_tokens, response.output_tokens)
```

## Parameters

| Parameter | Description |
|---|---|
| `model` | Required. Must be in the model list (`ellm_adapter.MODELS`). |
| `messages` | Required. `[{"role": "user" \| "assistant", "content": str}, ...]` |
| `system_prompt` | Optional. |
| `response_schema` | Optional. A Pydantic class. If given, `response.parsed` is an instance of it. |
| `temperature` | Optional. Sent only to models that support it. |
| `reasoning_effort` | Optional. Sent only if the value is one the model accepts (see `ellm_adapter.MODELS`). |
| `max_tokens` | Optional. Defaults to the model's maximum. Thinking tokens count toward this limit. |
| `fallback_model` | Optional. If the first call fails for any reason, the same request is sent to this model. |
| `timeout` | Optional. Seconds per model call, default 600. With a fallback, each call gets the full timeout. |

Fixed settings: no SDK retries, nothing stored on the provider side (`store=False`).

## Return value: `LLMResponse`

| Field | Description |
|---|---|
| `text` | The text the model produced. |
| `parsed` | The validated object if `response_schema` was given, otherwise `None`. |
| `provider`, `model` | The provider and model that actually answered. If the fallback was used, these are the fallback's. |
| `input_tokens` | Total input tokens, including cached ones. |
| `output_tokens` | Total tokens billed as output, including thinking. |
| `reasoning_tokens` | The thinking share of `output_tokens`. `None` if the provider doesn't report it. |
| `dropped_params` | Parameters not sent because the model doesn't support them, e.g. `["temperature"]`. |
| `primary_error` | The first model's error if the fallback was used, otherwise `None`. |
| `raw` | The provider's raw response. |

## Errors

| Error | When |
|---|---|
| `UnknownModelError` | `model` or `fallback_model` is not in the list. No request is sent. |
| `LLMResponseError` | A response arrived but can't be used. `reason`: `truncated`, `refused`, `blocked`, `invalid_output` or `other`. Token counts are on the error. |
| `FallbackError` | Both models failed. The `primary_error` and `fallback_error` fields hold both errors. |
| SDK errors | Without a fallback, the provider SDK's own error (rate limit, timeout, etc.) is raised as is. |

## Supported models

The list is `ellm_adapter.MODELS` (in `ellm_adapter/models.py`): for each model, its provider, maximum output tokens, whether it takes `temperature`, and which `reasoning_effort` values it accepts. A model that isn't listed can't be called; adding one is a single entry.

## Known limitations

- Synchronous, text only: no async client, streaming, tool use or image/file input yet. In async code, call it with `asyncio.to_thread(call_llm, ...)`.
- `temperature` is never sent to Anthropic models: Anthropic's SDK 2.x removed the parameter.
- OpenAI reasoning models accept `temperature` only with `reasoning_effort="none"`.
- Gemini 2.5 models set thinking with a token budget, which isn't supported, so `reasoning_effort` is dropped for them.
- Claude doesn't enforce constraints such as `minLength`, `maxLength`, `minimum` and `maximum` in Pydantic schemas, and OpenAI doesn't support `dict[str, X]` fields. The response is always validated with Pydantic after it arrives.

## Development

```bash
pip install -e ".[dev]"
python -m unittest discover tests        # offline: real SDKs against a local fake server, no keys, no cost
python tests/live_test.py [model ...]     # live: real APIs, costs a little, keys from .env
```

Run the offline tests after any change to the package, and the live test after changing `models.py` (`--all-efforts` also tries every listed `reasoning_effort` value).

To release: bump `__version__` in `src/ellm_adapter/__init__.py`, then `python -m build` and `twine upload dist/*`.

## License

[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). The full text ships with the package as `LICENSE`.
