airflow.providers.common.ai.batch.openai¶
OpenAI batch adapter.
Talks to the openai SDK directly rather than through
apache-airflow-providers-openai: that provider’s hook has no batch listing
or file-download methods, so reusing it would add a hard dependency without
the capabilities this needs. The SDK import is deferred to
_build_client() so importing this module never requires openai.
Attributes¶
Classes¶
Batch adapter for OpenAI's Batch API ( |
Module Contents¶
- class airflow.providers.common.ai.batch.openai.OpenAIBatchAdapter(*, api_key=None, base_url=None, client=None)[source]¶
Bases:
airflow.providers.common.ai.batch.base.BatchAdapterBatch adapter for OpenAI’s Batch API (
/v1/chat/completions).- Parameters:
api_key (str | None) – Passed straight to the
openai.OpenAIclient.Nonefalls back to the SDK’s own env-var resolution (OPENAI_API_KEY).base_url (str | None) – Passed straight to the
openai.OpenAIclient. Pointing it at an OpenAI-compatible gateway that exposes/v1/filesand/v1/batchesroutes the batch through that gateway.client (Any | None) – Inject a pre-built client (or a test double) instead of constructing one from
api_key/base_url.
- build_output_directive(spec)[source]¶
Translate
spec.json_schemainto this provider’s structured-output request fields.Returns
{}whenspec.is_structuredisFalse. The result is merged into every request’s body/params by the adapter itself.
- 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, completion_window='24h', **kwargs)[source]¶
Upload/submit the batch and return the provider’s batch id.
Where the provider offers batch-level metadata (OpenAI), 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.
- get_batch(batch_id)[source]¶
Return the current status of a submitted batch. Synchronous; the trigger wraps it in
to_thread.
- find_orphaned_batch(idempotency_key, input_fingerprint, not_before)[source]¶
Look for a batch whose metadata carries both identifiers, created at or after
not_before.submit()recordsidempotency_keyandinput_fingerprintin the batch’s metadata, so a batch whose submit response never made it back can be found and re-attached to. No server-side metadata filter exists, so this walks up to_ORPHAN_SCAN_LIMITbatches client-side; among every match the most recently created one wins.