> ## Documentation Index
> Fetch the complete documentation index at: https://astronomer.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# LLM extraction of structured data for revenue prediction with Apache Airflow®

> Learn how to build training and inference pipelines that extract structured features from unstructured text with an LLM and use them to train a traditional regression model.

## Overview

Extracting structured fields from free-form text is a common LLM use case. Downstream tasks can use fields extracted by AI in many different situations, for example loading them into a database or turning them into features for a traditional machine learning model, so in this pattern, the AI step is a helper for a conventional ETL or MLOps pipeline.

This architecture is an MLOps pipeline for a sales team predicting expected revenue on open deals with a regression model. Predicting a continuous value is a regression problem, which a traditional machine learning model is best suited for. The LLM's task is part of the feature engineering: turning call transcripts, sales emails, and account research into the numeric and categorical features the model needs.

<Tip>
  For general information on how to orchestrate LLM calls with Airflow, see [LLM orchestration with Apache Airflow®](/docs/learn/ai-orchestration-llm).
</Tip>

## Architecture

<Frame>
  <img src="https://mintcdn.com/astronomer/qmpJAusP85vNf2dq/images/img/reference_architectures/reference-architecture-structured-extraction_diagram.png?fit=max&auto=format&n=qmpJAusP85vNf2dq&q=85&s=c72e2d319329b339a0702b53b9b2c0cf" alt="Structured extraction reference architecture diagram. A monthly Dag extracts call transcripts, sales emails, account research, and known deal outcomes, pulls features with a mapped @task.llm call, and retrains a regression model. A daily Dag runs the same extraction on in-flight deals, runs model inference, and sends an expected revenue report." width="1360" height="997" data-path="images/img/reference_architectures/reference-architecture-structured-extraction_diagram.png" />
</Frame>

This architecture consists of two Dags that use parts of the same modularized code:

* **Monthly training Dag**: Extracts the unstructured sources plus the known deal outcomes, runs the LLM extraction, processes the extracted features, loads them as training data, splits into test and train sets, and retrains the model.
* **Daily inference Dag**: Extracts the same unstructured sources for deals with no known outcome yet, runs the same LLM extraction with the same output class, loads the features as inference data, runs the regression model, and sends an expected revenue report to sales leaders.

The code shared between the two Dags is stored in a module that both Dags import. If the training extraction and the inference extraction were to drift apart, the model would be served features that do not match what it learned from, and the predictions would degrade.

### Airflow features

* [**Dynamic task mapping**](/docs/learn/dynamic-tasks): Extraction is mapped over the list of deals, so both Dags scale with how many deals are in scope for that run.
* [**`@task.llm`**](/docs/learn/airflow-common-ai-provider#@task-llm): Runs the feature extraction as a single model call per input.
* [**Structured output**](/docs/learn/airflow-common-ai-provider#output-type): Because the `output_type` is defined as a Pydantic model, you can rely on the AI output fitting the defined schema for downstream tasks.
* [**Automatic retries**](/docs/learn/rerunning-dags#automatically-retry-tasks): Extraction calls retry per deal in case of rate limits.

## Considerations

* **Make sure the Dags use the same extraction step.** The training and inference paths have to produce an identical feature set. Put the output class, the prompt, and the formatting in one importable module.
* **Keep the extracted features inspectable.** Loading the intermediate features to a table helps with debugging when prediction accuracy drops over time.
* **Watch for changes in the unstructured sources.** The model might need retraining or changes in its feature engineering if the sales team changes how it writes call notes.
* **Version the output class alongside the model.** When the extraction fields change, the model needs retraining before the inference Dag uses the new fields.

## Next steps

* Look up decorator and operator parameters in [Orchestrate AI tasks with Apache Airflow® and the Common AI provider](/docs/learn/airflow-common-ai-provider).
* Fit this pipeline into a broader MLOps setup with [Best practices for orchestrating MLOps pipelines with Airflow](/docs/learn/airflow-mlops).
* Read the [AI Orchestration with Apache Airflow®](https://www.astronomer.io/ebooks/ai-orchestration-with-apache-airflow/) eBook for the full set of AI orchestration patterns.
* Deploy the Airflow pipelines with a [free trial of Astro](https://www.astronomer.io/lp/signup/).
