airflow.providers.common.ai.batch.pydantic_ai_backend

Batch adapters built on pydantic-ai’s public Model.batch_* methods.

One backend drives every provider pydantic-ai can batch: it encodes each request with batch_encode, submits them with batch_create, polls with batch_status, collects with batch_results and reads each result with batch_decode. The provider subclasses set only what differs between providers (the settings they accept and how a finished job is read).

pydantic-ai’s Batch handle is plain data. It is stored with the batch’s state record, so the trigger and a later worker rebuild a model from the connection and continue the same batch without the original process.

Importing this module never imports a provider SDK: the model classes are built by PydanticAIHook, and the SDK-specific settings types are imported inside the methods that need them.

Attributes

log

T

L

Classes

PydanticAIBatchAdapter

Batch adapter for any provider whose pydantic-ai Model implements the batch_* methods.

OpenAIBatchAdapter

Batch adapter for OpenAI's Batch API, on the Responses API (/v1/responses).

AnthropicBatchAdapter

Batch adapter for Anthropic's Message Batches API.

GoogleBatchAdapter

Batch adapter for the Gemini Developer API (Batch Mode).

Module Contents

airflow.providers.common.ai.batch.pydantic_ai_backend.log[source]
airflow.providers.common.ai.batch.pydantic_ai_backend.T[source]
airflow.providers.common.ai.batch.pydantic_ai_backend.L[source]
class airflow.providers.common.ai.batch.pydantic_ai_backend.PydanticAIBatchAdapter(*, hook=None, model_id=None, batch_handle=None, output_spec=None, model_factory=None)[source]

Bases: airflow.providers.common.ai.batch.base.BatchAdapter

Batch adapter for any provider whose pydantic-ai Model implements the batch_* methods.

Note

Experimental: this can change or be removed in a minor release of this provider. See Stable and experimental features.

The adapter is bound to one connection and one model_id. It builds a fresh Model for every operation and runs it in its own event loop, so no client crosses a loop, and it never keeps a connection open between operations.

Parameters:
conn_types[source]
accepts_raw_params: ClassVar[bool] = True[source]
input_file_key: ClassVar[str] = 'input_file'[source]
supports_completion_window = False[source]
classmethod from_connection(conn, *, model_id=None, batch_handle=None, output_spec=None)[source]

Build the adapter for a fetched connection.

The default reads password and host as api_key and base_url and ignores the keyword arguments, which is what an adapter that talks to a vendor SDK directly needs. An adapter that rebuilds a client from the batch’s recorded state overrides this.

Parameters:
validate_requests(requests, *, model, output_spec, idempotency_key, system_prompt='', max_tokens=1024, request_params=None, **kwargs)[source]

Reject an over-limit or otherwise invalid batch before any network call.

Must check len(requests) against max_requests and the serialized request size (including the per-request output directive built from output_spec, which a large schema repeats in every request) against max_payload_bytes, and call check_custom_id_length(). Raise a subclass of LLMBatchInputError naming the limit, the actual count/size, and the .expand() remedy.

**kwargs carries the same batch-level request-building context as submit() (system_prompt, max_tokens, request_params) so the serialized-size estimate matches what submit sends.

submit(requests, *, model, idempotency_key, input_fingerprint, output_spec, system_prompt='', max_tokens=1024, request_params=None, **kwargs)[source]

Upload/submit the batch and return the provider’s batch id.

Where the provider offers batch-level metadata, record both idempotency_key and input_fingerprint there so find_orphaned_batch() can read them back. The key alone identifies the task instance, not this submission of it: it is stable across a clear even when the prompts changed.

async aget_batch(batch_id)[source]

Async counterpart of get_batch(), which the trigger calls. Runs the sync one in a thread by default.

get_batch(batch_id)[source]

Return the current status of a submitted batch. Synchronous; the trigger wraps it in to_thread.

async acancel_batch(batch_id)[source]

Async counterpart of cancel_batch(), which the trigger calls. Runs the sync one in a thread by default.

cancel_batch(batch_id)[source]

Request cancellation of a batch.

find_orphaned_batch(idempotency_key, input_fingerprint, not_before)[source]

Return None unconditionally: batch_create takes no metadata to find a batch by.

A crash between “submit sent” and “response recorded” falls through to on_orphaned_intent.

iter_results(batch_id)[source]

Return (not yield) a streaming iterator of per-request results.

Returning a plain iterator rather than using yield in this method makes an invalid batch_id (or any other call-time failure) raise immediately, instead of only once the caller starts iterating.

extract_output(raw, spec)[source]

Pull the model’s output out of a provider-native result, in the shape ExtractedOutput describes.

class airflow.providers.common.ai.batch.pydantic_ai_backend.OpenAIBatchAdapter(*, hook=None, model_id=None, batch_handle=None, output_spec=None, model_factory=None)[source]

Bases: PydanticAIBatchAdapter

Batch adapter for OpenAI’s Batch API, on the Responses API (/v1/responses).

pydantic-ai batches only its Responses model, so an openai-chat: model id is built as the openai: model of the same name and logged. Azure OpenAI has no adapter. A batch’s group includes the model, so every request must use the same one. Raw Responses fields in request_params (user, metadata, …) go through extra_body, so use Responses names: openai_reasoning_effort or reasoning={"effort": ...}, not reasoning_effort. A Chat Completions parameter that has a Responses equivalent fails before submission.

A batch gateway that implements /v1/files and /v1/batches for /v1/responses works through the connection’s host; one that implements those only for /v1/chat/completions does not.

name = 'openai'[source]
max_requests = 50000[source]
max_payload_bytes = 200000000[source]
allows_per_request_model = False[source]
input_file_key = 'input_file_id'[source]
async acancel_batch(batch_id)[source]

Async counterpart of cancel_batch(), which the trigger calls. Runs the sync one in a thread by default.

class airflow.providers.common.ai.batch.pydantic_ai_backend.AnthropicBatchAdapter(*, hook=None, model_id=None, batch_handle=None, output_spec=None, model_factory=None)[source]

Bases: PydanticAIBatchAdapter

Batch adapter for Anthropic’s Message Batches API.

The batch’s group leaves the model out, so one batch can hold requests for different Claude models. Raw Messages API fields in request_params (metadata, thinking, …) go through extra_body.

name = 'anthropic'[source]
max_requests = 100000[source]
max_payload_bytes = 256000000[source]
allows_per_request_model = True[source]
class airflow.providers.common.ai.batch.pydantic_ai_backend.GoogleBatchAdapter(*, hook=None, model_id=None, batch_handle=None, output_spec=None, model_factory=None)[source]

Bases: PydanticAIBatchAdapter

Batch adapter for the Gemini Developer API (Batch Mode).

Vertex AI is not supported, because its batch prediction reads and writes Cloud Storage or BigQuery. The Gemini batch API has no raw request passthrough, so request_params may only hold typed settings. A batch’s group includes the model, so every request must use the same one.

name = 'google'[source]
max_requests = 9223372036854775807[source]
max_payload_bytes = 2000000000[source]
allows_per_request_model = False[source]
accepts_raw_params = False[source]

Was this entry helpful?