Metadata-Version: 2.5
Name: genkit-openai
Version: 0.13.0
Summary: OpenAI-compatible models on Genkit
Project-URL: Bug Tracker, https://github.com/genkit-ai/genkit/issues
Project-URL: Changelog, https://github.com/genkit-ai/genkit/releases
Project-URL: Documentation, https://genkit.dev/docs/get-started/?lang=python
Project-URL: Homepage, https://github.com/genkit-ai/genkit
Project-URL: Repository, https://github.com/genkit-ai/genkit/tree/main/py
Author: Google
Author-email: Yesudeep Mangalapilly <yesudeep@google.com>, Elisa Shen <mengqin@google.com>, Niraj Nepal <nnepal@google.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: genkit,openai
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Environment :: Web Environment
Classifier: Framework :: AsyncIO
Classifier: Framework :: Pydantic
Classifier: Framework :: Pydantic :: 2
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: genkit<2,>=0.13.0
Requires-Dist: openai<3,>=1.60.0
Requires-Dist: strenum>=0.4.15; python_version < '3.11'
Description-Content-Type: text/markdown

# Genkit OpenAI Plugin

OpenAI-compatible model provider for Genkit (OpenAI, Azure OpenAI, and other
compatible endpoints).

> **Building with a coding agent? Install the Genkit Python skill first.**
>
> ```bash
> npx skills add genkit-ai/skills --skill developing-genkit-python
> ```
>
> It teaches your agent the current Genkit Python APIs and common gotchas.
> Source, manual install and skills for other languages:
> [genkit-ai/skills](https://github.com/genkit-ai/skills).

## Installation

```bash
uv add genkit-openai
```

## Usage

```python
from genkit import Genkit
from genkit_openai import OpenAI

ai = Genkit(plugins=[OpenAI()])

res = await ai.generate(
    model=OpenAI.gpt('gpt-5.2'),
    prompt='Suggest 2 catchy names for an AI newsletter.',
)
print(res.text)
```

Set `OPENAI_API_KEY` in the environment, or pass `api_key=` to `OpenAI()`.
`base_url=` points the plugin at another OpenAI-compatible server. Pass the URL
up to and including `/v1`; the SDK appends `/chat/completions`. Any other
`AsyncOpenAI` setting goes in `client_options`:

```python
OpenAI(
    api_key=key,
    base_url='https://openrouter.ai/api/v1',
    client_options={'organization': 'org-123', 'timeout': 30, 'max_retries': 5},
)
```

## Config

`config` takes the settings `OpenAIConfig` declares. Any other key raises
before the request is sent, so a typo fails by name:

```python
res = await ai.generate(
    model='openai/gpt-4o',
    prompt='Write a haiku about the sea.',
    config={
        'temperature': 0.2,
        'max_output_tokens': 200,  # caps reply length
        'extra': {'reasoning': {'effort': 'low'}},  # fields OpenAIConfig doesn't declare
    },
)
```

- `max_output_tokens` (`maxOutputTokens` in the Dev UI) caps reply length.
  It goes out as `max_tokens`, or as `max_completion_tokens` on reasoning
  models. `stop_sequences` goes out as `stop`. OpenAI's own names
  (`max_tokens`, `max_completion_tokens`, `stop`) are not declared on
  `OpenAIConfig`, so passing them at the top level raises `INVALID_ARGUMENT`.
- `extra` goes out as top-level request body fields. A key in `extra`
  replaces a setting of the same name (including `max_tokens` or
  `max_completion_tokens` for OpenAI-compatible servers). An API key in
  `config` or `extra` raises `INVALID_ARGUMENT`; it belongs in
  `context.secrets`.

## Per-request API key

To bill a call to someone else's OpenAI account, pass their key in
`context.secrets`. It's used for that call only, on chat, image,
text-to-speech, and transcription models:

```python
res = await ai.generate(
    model='openai/gpt-4o',
    prompt='hi',
    context={'secrets': {'api_key': tenant_key}},
)
```

- `apiKey` works as well as `api_key`. Other entries in `context.secrets`
  are left alone, so a call whose secrets hold no key runs on the plugin's
  key. A key that is blank or not a string raises `INVALID_ARGUMENT`.
- The tenant call sends only the tenant's key. The plugin's `organization`,
  `project`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` are left off; `base_url`,
  timeouts and other `default_headers` carry over. A plugin whose
  `default_headers` pin `Authorization` refuses a tenant key with
  `FAILED_PRECONDITION`.
- The plugin doesn't need a key of its own for chat, image, text-to-speech,
  and transcription models: `OpenAI()` with no `OPENAI_API_KEY` serves every
  call that brings a key in `context.secrets`, and a call without one fails
  with `FAILED_PRECONDITION`.
- Embedders run on the plugin's key only, since `ai.embed()` takes no
  `context`. On a plugin without a key they fail with `FAILED_PRECONDITION`.
