Skip to main content
Fireworks AI is a platform for running inference on AI models and fine-tuning open models. You can call models on shared serverless infrastructure, run them on dedicated GPU deployments, and fine-tune models on your own datasets. Apache Airflow® lets you orchestrate all of these actions as part of your Dags. In this guide you’ll learn:
  • How to connect Airflow to Fireworks AI.
  • How to run serverless inference with the Common AI provider.
  • How to run inference on a dedicated Fireworks AI deployment that scales to zero between Dag runs.
  • How to fine-tune a model on Fireworks AI.

Assumed knowledge

To get the most out of this guide, make sure you have an understanding of:

Why use Fireworks AI with Airflow?

Using Fireworks AI together with Airflow gives you the following advantages:
  • Flexible scheduling: Orchestrate any Fireworks AI action on a time-based, data-aware, or event-driven schedule. See Schedule Dags in Airflow.
  • Durable execution with Airflow: Fine-tuning jobs often take several hours or longer. When you save the Fireworks AI job ID in the task state store, a task retry reconnects to the running job and doesn’t start a second one. See Durable execution with the task state store.
  • Deferrable operators: A deferrable operator defers waiting for a fine-tuning job to the triggerer, which frees the worker slot until the fine-tuning job finishes. See Deferrable operators.
  • Setup and teardown tasks: Scale a dedicated deployment up in a setup task and back down in a teardown task. As long as the setup task succeeded, the teardown task runs even when the inference task fails. See Use setup and teardown tasks in Airflow.
  • Dynamic task mapping: Run one inference task per file, record, or prompt, with the number of tasks determined at runtime. See Create dynamic Airflow tasks.
  • Common AI provider: The Common AI provider includes several operators and decorators (for example, @task.llm and @task.agent) that call Fireworks AI models and return structured output. This guide uses the Common AI provider to run serverless inference and to run inference on a dedicated deployment. See Common AI provider.

Ways to use Fireworks AI with Airflow

You can use Fireworks AI in Airflow in the following ways:
  • Call the Fireworks AI REST API or use the Fireworks AI Python SDK in a @task. This works for every Fireworks AI feature: inference, datasets, fine-tuning jobs, and deployments. To reuse the same logic across Dags, wrap it in a custom operator, which you can make durable, deferrable, or both.
  • Use the Common AI provider, which is based on PydanticAI, for inference. This works with serverless models and dedicated deployments, for both LLM calls and AI agents.
Airflow works with any harness that you can run in Python. If your harness, for example LangChain or CrewAI, supports Fireworks AI, you can run it inside a @task. See Start with the harness you already have.

Connect to Fireworks AI

How you provide your Fireworks AI API key depends on how you call Fireworks AI. The Common AI provider reads the key from an Airflow connection. When you use the Fireworks AI Python SDK or the API in a @task, you can read the key from the same connection or from the FIREWORKS_API_KEY environment variable.

Use a Fireworks AI API key directly

To use the Fireworks AI Python SDK or the API directly in a @task, set your API key and account ID as environment variables.
  1. Create an API key in the Fireworks AI console.
  2. Copy your Fireworks AI account ID.
  3. If you want to use the Fireworks AI Python SDK, add it to your requirements.txt file.
  4. Set the API key and the account ID as environment variables. If you run Airflow locally with the Astro CLI, add them to your .env file. On Astro, set them in the Astro Environment Manager.

Use the Common AI provider

The Common AI provider reads your API key and a default model from an Airflow connection of the pydanticai type.
  1. Add the Common AI provider with the openai extra to your requirements.txt file. The Common AI provider uses PydanticAI to call models, and the Fireworks AI integration in PydanticAI uses the OpenAI client.
  2. Add the connection and your Fireworks AI account ID to your .env file. Use the pydanticai connection type, your Fireworks AI API key as the password, and a default model in the extra field in the format fireworks:<model>.
PydanticAI sends requests for models with the fireworks: prefix to https://api.fireworks.ai/inference/v1, so you don’t need to set a host. You can override the default model on each task with the model_id parameter.

Run serverless inference

Serverless models run on infrastructure that Fireworks AI shares between customers. See the Fireworks AI model library for all available models.

LLM orchestration

The following Dag retrieves the user prompt with an upstream task, passes it to Fireworks AI, and returns the LLM output as a structured summary, which the downstream task prints.
For use cases and more information on how to orchestrate LLM calls with Airflow, see LLM orchestration with Apache Airflow®.
This Dag has three tasks:
  • get_user_prompt: Returns the user prompt. You can use any Python code to retrieve the prompt from any location.
  • ask_question: Uses @task.llm to send the prompt and a system prompt to gpt-oss-120b through the fireworks_default connection. The task returns the model response as a ToolSummary object.
  • print_response: Prints the name and summary from the model response. Airflow lets you use the model output in downstream tasks, for example to route data to different branches, classify records, or write the results to a database. Airflow UI graph view of a successful run of the fireworks_ai_inference_llm Dag with the tasks get_user_prompt, ask_question, which uses @task.llm, and print_response

Agent orchestration

The following Dag retrieves the user prompt with an upstream task, passes it to an AI agent that runs on Fireworks AI, and returns the agent output as a structured result, which the downstream task prints. Agents can be given access to tools. Any Python function can be run as a tool by an agent.
For use cases and more information on how to orchestrate AI agents with Airflow, see Agent orchestration with Apache Airflow®.
This Dag has three tasks:
  • get_user_prompt: Returns the user prompt. You can use any Python code to retrieve the prompt from any location.
  • pick_number: Uses @task.agent to run an agent that uses gpt-oss-120b. The get_random_number function is passed to the agent in a FunctionToolset with the toolsets parameter. See Toolsets for more information. The task returns the agent output as a RandomNumberResult object.
  • print_response: Prints the number and whether it’s prime. Airflow lets you use the agent output in downstream tasks, for example to route data to different branches, classify records, or write the results to a database. Airflow UI graph view of a successful run of the fireworks_ai_inference_agent Dag with the tasks get_user_prompt, pick_number, which uses @task.agent, and print_response

Run inference on a dedicated deployment

A dedicated deployment runs a model on GPU capacity reserved for your Fireworks AI account. The following Dag scales a dedicated deployment up to one replica, runs inference on it, and scales the deployment back down to zero replicas at the end of the Dag run, even if inference fails. If the deployment doesn’t exist yet, the Dag creates it.
This Dag has four tasks:
  • scale_up_deployment: A setup task that scales the deployment in DEPLOYMENT_ID up to one replica. If the deployment doesn’t exist yet, the task creates it.
  • wait_for_replica: A sensor in reschedule mode that checks the deployment every 30 seconds until a replica is ready.
  • ask_question: Uses @task.llm to send a prompt to the deployment.
  • scale_down_deployment: A teardown task that scales the deployment to zero replicas, even if ask_question fails.
If the teardown task doesn’t run, Fireworks AI scales a deployment with a minimum of zero replicas down to zero after it receives no requests for the duration in scale_to_zero_window. The value must be at least 5 minutes, and the default is 1 hour.
Airflow UI graph view of a running fireworks_ai_deployment_sdk Dag run, where scale_up_deployment succeeded and the wait_for_replica sensor is up for reschedule while it waits for a ready replica

Fine-tune a model

Supervised fine-tuning on Fireworks AI takes a dataset in your account and a base model, and creates a new model in your account. A fine-tuning run in Airflow has three steps:
  1. Upload the training data to Fireworks AI as a dataset.
  2. Start a fine-tuning job.
  3. Wait for the job to finish and save the name of the fine-tuned model, so that other tasks can use it, for example for inference.
To run inference on the fine-tuned model, deploy it on a dedicated deployment.

Data import and export

Fireworks AI stores training data as datasets in your account. Fine-tuning jobs for chat models expect a JSONL file with one conversation per line, in the OpenAI chat format.
Training data can include images as well as text. Fireworks AI expects each image as a base64-encoded data URI in a user message. See Vision training in the Fireworks AI documentation.
The following Dag uploads training data to a new Fireworks AI dataset with the Fireworks AI Python SDK.
The upload_training_data task returns the dataset name, which a fine-tuning task can use as input. To export a dataset, for example to archive the exact training data of a model or to copy it to object storage, request signed download URLs with the SDK and download each file. The following Dag downloads one dataset to the include folder:
To export a dataset, you request signed download URLs for its files from Fireworks AI. The signed URLs expire, see Get Dataset Download Endpoint in the Fireworks AI documentation. Astronomer recommends requesting the signed URLs and downloading the files in the same Dag run.

Start the fine-tuning job and wait for it to finish

The following code starts a fine-tuning job on Fireworks AI and waits for it to finish.
  • start_fine_tuning_job: A durable task that starts the job and saves the job ID in the task state store. If the task fails after the job started and Airflow retries it, the retry reads the job ID and reuses the job, unless the job already failed, was cancelled, or expired.
  • wait_for_fine_tuning_job: A sensor in reschedule mode, which releases the worker slot between checks. It fails if the job fails and returns the job as a dictionary when the job is complete.