airflow.providers.openai.hooks.openai¶
Attributes¶
Classes¶
Enum for the status of a batch. |
|
Enum for the |
|
Use OpenAI SDK to interact with OpenAI APIs. |
Functions¶
|
Build (but do not raise) the exception matching a trigger event's termination reason. |
|
Validate the event a deferred task resumes with, returning it if well-formed. |
Module Contents¶
- class airflow.providers.openai.hooks.openai.TerminationReason[source]¶
-
Enum for the
termination_reasonfield of a trigger’s terminal event.
- airflow.providers.openai.hooks.openai.build_batch_error(message, termination_reason)[source]¶
Build (but do not raise) the exception matching a trigger event’s termination reason.
termination_reasonisNonewhen the event was produced by a trigger serialized before this field existed (a rolling upgrade in flight); that case falls back toOpenAIBatchJobException, matching today’s behavior.
- airflow.providers.openai.hooks.openai.validate_execute_complete_event(event=None)[source]¶
Validate the event a deferred task resumes with, returning it if well-formed.
The event crosses the triggerer/worker boundary through the metadata DB, so a resuming task can receive
Noneor a status its handler does not recognize (version skew, a custom trigger). Both must fail loudly instead of crashing opaquely or being misread as an outcome.
- class airflow.providers.openai.hooks.openai.OpenAIHook(conn_id=default_conn_name, *args, **kwargs)[source]¶
Bases:
airflow.providers.common.compat.sdk.BaseHookUse OpenAI SDK to interact with OpenAI APIs.
- Parameters:
conn_id (str) – OpenAI connection id
- get_conn()[source]¶
Return an OpenAI connection object.
The authentication mechanism is selected with the
auth_typekey in the connectionextra(default"api_key"):"api_key"– use the API key from the connection password (oropenai_client_kwargs)."workload_identity"– exchange a short-lived identity token for API access using the OpenAI client’s workload identity support. See_build_workload_identity().
- create_chat_completion(messages, model='gpt-4o-mini', **kwargs)[source]¶
Create a model response for the given chat conversation and returns a list of chat completions.
- Parameters:
messages (list[openai.types.chat.ChatCompletionSystemMessageParam | openai.types.chat.ChatCompletionUserMessageParam | openai.types.chat.ChatCompletionAssistantMessageParam | openai.types.chat.ChatCompletionToolMessageParam | openai.types.chat.ChatCompletionFunctionMessageParam]) – A list of messages comprising the conversation so far
model (str) – ID of the model to use
- create_response(input, model='gpt-4o-mini', **kwargs)[source]¶
Create a model response using the Responses API.
- Parameters:
input (Any) – Text, image, or file input(s) to the model.
model (str) – ID of the model to use.
- parse_response(input, model='gpt-4o-mini', *, text_format, **kwargs)[source]¶
Create a model response and parse it into a Pydantic model via the Responses API.
Wraps
openai.resources.responses.Responses.parse(). The SDK convertstext_formatinto a JSON schema, sends it as a structured-output request, and returns aParsedResponsewhoseoutput_parsedattribute is an instance oftext_format, orNonewhen the response carries no parsed output (for example a refusal).- Parameters:
input (Any) – Text, image, or file input(s) to the model.
model (str) – ID of the model to use.
text_format (type[_TextFormatT]) – A Pydantic
BaseModelsubclass describing the expected structured output. Keyword-only, as in the SDK. The SDK converts it to a JSON schema and sends the structured-output request.
- get_response(response_id, **kwargs)[source]¶
Retrieve a previously created model response.
- Parameters:
response_id (str) – The ID of the response to retrieve.
- delete_response(response_id)[source]¶
Delete a model response.
- Parameters:
response_id (str) – The ID of the response to delete.
- cancel_response(response_id)[source]¶
Cancel an in-progress response created with
background=True.- Parameters:
response_id (str) – The ID of the response to cancel.
- create_conversation(**kwargs)[source]¶
Create a conversation that can be reused across responses to persist state.
- get_conversation(conversation_id)[source]¶
Retrieve a conversation.
- Parameters:
conversation_id (str) – The ID of the conversation to retrieve.
- delete_conversation(conversation_id)[source]¶
Delete a conversation.
- Parameters:
conversation_id (str) – The ID of the conversation to delete.
- create_assistant(model='gpt-4o-mini', **kwargs)[source]¶
Create an OpenAI assistant using the given model.
- Parameters:
model (str) – The OpenAI model for the assistant to use.
- get_assistant(assistant_id)[source]¶
Get an OpenAI assistant.
- Parameters:
assistant_id (str) – The ID of the assistant to retrieve.
- modify_assistant(assistant_id, **kwargs)[source]¶
Modify an existing Assistant object.
- Parameters:
assistant_id (str) – The ID of the assistant to be modified.
- delete_assistant(assistant_id)[source]¶
Delete an OpenAI Assistant for a given ID.
- Parameters:
assistant_id (str) – The ID of the assistant to delete.
- delete_thread(thread_id)[source]¶
Delete an OpenAI thread for a given thread_id.
- Parameters:
thread_id (str) – The ID of the thread to delete.
- get_messages(thread_id, **kwargs)[source]¶
Return a list of messages for a given Thread.
- Parameters:
thread_id (str) – The ID of the thread the messages belong to.
- modify_message(thread_id, message_id, **kwargs)[source]¶
Modify an existing message for a given Thread.
- Parameters:
thread_id (str) – The ID of the thread to which this message belongs.
message_id – The ID of the message to modify.
- create_run(thread_id, assistant_id, **kwargs)[source]¶
Create a run for a given thread and assistant.
- create_run_and_poll(thread_id, assistant_id, **kwargs)[source]¶
Create a run for a given thread and assistant and then polls until completion.
- get_runs(thread_id, **kwargs)[source]¶
Return a list of runs belonging to a thread.
- Parameters:
thread_id (str) – The ID of the thread the run belongs to.
- create_embeddings(text: str | list[int], model: str = 'text-embedding-3-small', **kwargs: Any) list[float][source]¶
- create_embeddings(text: list[str] | list[list[int]], model: str = 'text-embedding-3-small', **kwargs: Any) list[list[float]]
Generate embeddings for the given text using the given model.
- Parameters:
text – The text to generate embeddings for.
model – The model to use for generating embeddings.
- Returns:
One embedding for a single text or token array; one embedding per item for a batch.
- upload_file(file, purpose)[source]¶
Upload a file that can be used across various endpoints. The size of all the files uploaded by one organization can be up to 100 GB.
- Parameters:
file (str) – The File object (not file name) to be uploaded.
purpose (Literal['fine-tune', 'assistants', 'batch']) – The intended purpose of the uploaded file. Use “fine-tune” for Fine-tuning, “assistants” for Assistants and Messages, and “batch” for Batch API.
- get_file(file_id)[source]¶
Return information about a specific file.
- Parameters:
file_id (str) – The ID of the file to use for this request.
- delete_file(file_id)[source]¶
Delete a file.
- Parameters:
file_id (str) – The ID of the file to be deleted.
- get_vector_store(vector_store_id)[source]¶
Retrieve a vector store.
- Parameters:
vector_store_id (str) – The ID of the vector store to retrieve.
- modify_vector_store(vector_store_id, **kwargs)[source]¶
Modify a vector store.
- Parameters:
vector_store_id (str) – The ID of the vector store to modify.
- delete_vector_store(vector_store_id)[source]¶
Delete a vector store.
- Parameters:
vector_store_id (str) – The ID of the vector store to delete.
- upload_files_to_vector_store(vector_store_id, files)[source]¶
Upload files to a vector store and poll until completion.
- get_vector_store_files(vector_store_id)[source]¶
Return a list of vector store files.
- Parameters:
vector_store_id (str)
- delete_vector_store_file(vector_store_id, file_id)[source]¶
Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use delete_file.
- create_batch(file_id, endpoint, metadata=None, completion_window='24h', **kwargs)[source]¶
Create a batch for a given model and files.
- Parameters:
file_id (str) – The ID of the file to be used for this batch.
endpoint (str) – The endpoint to use for this batch. Allowed values are determined by the OpenAI Batch API; see https://platform.openai.com/docs/api-reference/batch/create for the current list.
metadata (dict[str, str] | None) – A set of key-value pairs that can be attached to an object.
completion_window (Literal['24h']) – The time window for the batch to complete. Default is 24 hours.
- get_batch(batch_id)[source]¶
Get the status of a batch.
- Parameters:
batch_id (str) – The ID of the batch to get the status of.
- wait_for_batch(batch_id, wait_seconds=3, timeout=3600)[source]¶
Poll a batch to check if it finishes.
- cancel_batch(batch_id)[source]¶
Cancel a batch.
- Parameters:
batch_id (str) – The ID of the batch to delete.
- create_agent(**kwargs)[source]¶
Create a reusable Managed Agent using the SDK’s agent configuration arguments.
- create_agent_session(*, input, environment, **kwargs)[source]¶
Create a fresh Managed Agents session and submit its initial turn.
- get_agent_session(session_id)[source]¶
Retrieve a Managed Agents session, including required actions and usage.
- cancel_agent_session(session_id)[source]¶
Request cancellation of the session’s active turn, preserving its history and artifacts.
- poll_agent_session(session_id)[source]¶
Check the first turn of a fresh, exclusively owned session.
Return a terminal result, or
Nonewhile waiting. An idle session without a visible turn is not completion: the submitted input may still be queued. This helper must not be used to wait for subsequent turns of a reused session.