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¶
Classes¶
Batch adapter for any provider whose pydantic-ai |
|
Batch adapter for OpenAI's Batch API, on the Responses API ( |
|
Batch adapter for Anthropic's Message Batches API. |
|
Batch adapter for the Gemini Developer API (Batch Mode). |
Module Contents¶
- 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.BatchAdapterBatch adapter for any provider whose pydantic-ai
Modelimplements thebatch_*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 freshModelfor 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:
hook (airflow.providers.common.ai.hooks.pydantic_ai.PydanticAIHook | None) – The hook the models are built from (credentials,
provider_kwargs, the connection’s extra).model_id (str | None) – The batch’s
"<provider>:<model>"id.batch_handle (str | None) – The serialized
Batchhandle recorded when the batch was submitted.output_spec (airflow.providers.common.ai.batch.output_schema.OutputSpec | None) – The task’s output spec. Needed only to read results.
model_factory (collections.abc.Callable[[str], pydantic_ai.models.Model] | None) – Builds the
Modelfor a model id instead of the hook. For tests.
- classmethod from_connection(conn, *, model_id=None, batch_handle=None, output_spec=None)[source]¶
Build the adapter for a fetched connection.
The default reads
passwordandhostasapi_keyandbase_urland 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:
model_id (str | None) – The batch’s
"<provider>:<model>"id.batch_handle (str | None) – The
SubmitResult.handlerecorded at submit time, if any.output_spec (airflow.providers.common.ai.batch.output_schema.OutputSpec | None) – The task’s output spec, for an adapter that decodes results.
- 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)againstmax_requestsand the serialized request size (including the per-request output directive built fromoutput_spec, which a large schema repeats in every request) againstmax_payload_bytes, and callcheck_custom_id_length(). Raise a subclass ofLLMBatchInputErrornaming the limit, the actual count/size, and the.expand()remedy.**kwargscarries the same batch-level request-building context assubmit()(system_prompt,max_tokens,request_params) so the serialized-size estimate matches whatsubmitsends.
- 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_keyandinput_fingerprintthere sofind_orphaned_batch()can read them back. The key alone identifies the task instance, not this submission of it: it is stable across acleareven 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.
- find_orphaned_batch(idempotency_key, input_fingerprint, not_before)[source]¶
Return
Noneunconditionally:batch_createtakes no metadata to find a batch by.A crash between “submit sent” and “response recorded” falls through to
on_orphaned_intent.
- 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:
PydanticAIBatchAdapterBatch 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 theopenai: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 inrequest_params(user,metadata, …) go throughextra_body, so use Responses names:openai_reasoning_effortorreasoning={"effort": ...}, notreasoning_effort. A Chat Completions parameter that has a Responses equivalent fails before submission.A batch gateway that implements
/v1/filesand/v1/batchesfor/v1/responsesworks through the connection’shost; one that implements those only for/v1/chat/completionsdoes not.
- 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:
PydanticAIBatchAdapterBatch 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 throughextra_body.
- 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:
PydanticAIBatchAdapterBatch 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_paramsmay only hold typed settings. A batch’s group includes the model, so every request must use the same one.