# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Operator for analyzing files with LLMs."""
from __future__ import annotations
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, ClassVar
from pydantic import BaseModel
from airflow.providers.common.ai.operators.llm import LLMOperator
from airflow.providers.common.ai.utils.file_analysis import build_file_analysis_request
from airflow.providers.common.ai.utils.logging import log_run_summary
from airflow.providers.common.ai.utils.usage import coerce_usage_limits
if TYPE_CHECKING:
from pydantic_ai import Agent
from airflow.sdk import Context
[docs]
class LLMFileAnalysisOperator(LLMOperator):
"""
Analyze files from object storage or local storage using a single LLM call.
The operator resolves ``file_path`` via
:class:`~airflow.providers.common.compat.sdk.ObjectStoragePath`, normalizes
supported formats into text context, and optionally attaches images/PDFs as
multimodal inputs when ``multi_modal=True``.
:param prompt: The analysis prompt for the LLM.
:param llm_conn_id: Connection ID for the LLM provider.
:param model_id: Model identifier (e.g. ``"openai:gpt-5"``).
Overrides the model stored in the connection's extra field.
:param fallback_conn_ids: Connection IDs to fail over to, in order, when
the primary provider is unavailable. Overrides the ``fallback_conn_ids``
set in the connection's extra field. ``None`` (default) reads the
connection's own extra field; an explicit ``[]`` disables a chain
configured there. See
:class:`~airflow.providers.common.ai.hooks.pydantic_ai.PydanticAIHook`
for how blank entries in the list are dropped.
:param system_prompt: Additional instructions appended to the built-in
file-analysis system prompt.
:param agent_params: Additional keyword arguments passed to the pydantic-ai
``Agent`` constructor (e.g. ``retries``, ``model_settings``, ``tools``).
:param file_path: File or prefix to analyze.
:param file_conn_id: Optional connection ID for the storage backend.
Overrides a connection embedded in ``file_path``.
:param multi_modal: Allow PNG/JPG/PDF inputs as binary attachments.
Default ``False``.
:param max_files: Maximum number of files to include from a prefix.
Excess files are omitted and noted in the prompt. Default ``20``.
:param max_file_size_bytes: Maximum size of any single input file.
Default ``5 MiB``.
:param max_total_size_bytes: Maximum cumulative size across all resolved
files. Default ``20 MiB``.
:param max_text_chars: Maximum normalized text context passed to the LLM
after sampling/truncation. Default ``100000``.
:param sample_rows: Maximum number of sampled rows or records included for
CSV, Parquet, and Avro inputs. This limits structural preview depth,
while ``max_file_size_bytes`` and ``max_total_size_bytes`` limit bytes
read from storage and ``max_text_chars`` limits the final prompt text
budget. Default ``10``.
``usage_limits`` is inherited from
:class:`~airflow.providers.common.ai.operators.llm.LLMOperator`.
Human-in-the-Loop approval parameters are inherited from
:class:`~airflow.providers.common.ai.operators.llm.LLMOperator`
(``require_approval``, ``approval_timeout``, ``on_approval_timeout``,
``allow_modifications``, ``approval_notifiers``, ``approval_assigned_users``).
The task pauses after the file analysis and only returns the result once a
reviewer approves.
"""
[docs]
template_fields: Sequence[str] = (
*LLMOperator.template_fields,
"file_path",
"file_conn_id",
)
# Runs its own execute() without the confidence gate; a decision_policy is rejected at construction.
[docs]
supports_decision_policy: ClassVar[bool] = False
def __init__(
self,
*,
file_path: str,
file_conn_id: str | None = None,
multi_modal: bool = False,
max_files: int = 20,
max_file_size_bytes: int = 5 * 1024 * 1024,
max_total_size_bytes: int = 20 * 1024 * 1024,
max_text_chars: int = 100_000,
sample_rows: int = 10,
**kwargs: Any,
) -> None:
super().__init__(**kwargs)
if max_files <= 0:
raise ValueError("max_files must be greater than zero.")
if max_file_size_bytes <= 0:
raise ValueError("max_file_size_bytes must be greater than zero.")
if max_total_size_bytes <= 0:
raise ValueError("max_total_size_bytes must be greater than zero.")
if max_text_chars <= 0:
raise ValueError("max_text_chars must be greater than zero.")
if sample_rows <= 0:
raise ValueError("sample_rows must be greater than zero.")
[docs]
self.file_path = file_path
[docs]
self.file_conn_id = file_conn_id
[docs]
self.multi_modal = multi_modal
[docs]
self.max_files = max_files
[docs]
self.max_file_size_bytes = max_file_size_bytes
[docs]
self.max_total_size_bytes = max_total_size_bytes
[docs]
self.max_text_chars = max_text_chars
[docs]
self.sample_rows = sample_rows
[docs]
def execute(self, context: Context) -> Any:
# Coerced first so a bad rendered value fails before the expensive setup below.
usage_limits = coerce_usage_limits(self.usage_limits)
if not isinstance(self.prompt, str):
raise TypeError(
f"{type(self).__name__} requires a string prompt (got {type(self.prompt).__name__}). "
"Supply images or PDFs via file_path with multi_modal=True instead."
)
request = build_file_analysis_request(
file_path=self.file_path,
file_conn_id=self.file_conn_id,
prompt=self.prompt,
multi_modal=self.multi_modal,
max_files=self.max_files,
max_file_size_bytes=self.max_file_size_bytes,
max_total_size_bytes=self.max_total_size_bytes,
max_text_chars=self.max_text_chars,
sample_rows=self.sample_rows,
)
self.log.info(
"Calling model for file analysis: files=%s, attachments=%s, text_files=%s, total_size_bytes=%s, "
"omitted_files=%s, text_truncated=%s, multi_modal=%s, sample_rows=%s",
len(request.resolved_paths),
request.attachment_count,
request.text_file_count,
request.total_size_bytes,
request.omitted_files,
request.text_truncated,
self.multi_modal,
self.sample_rows,
)
self.log.debug("Resolved file analysis paths: %s", request.resolved_paths)
agent: Agent[object, Any] = self.llm_hook.create_agent(
output_type=self.output_type,
instructions=self._build_system_prompt(),
**self.agent_params,
)
result = self.run_agent_sync(agent, request.user_content, usage_limits=usage_limits)
log_run_summary(self.log, result)
output = result.output
if self.require_approval:
self.defer_for_approval(context, output) # type: ignore[misc]
if self._serialize_model_output and isinstance(output, BaseModel):
# Honour ``serialize_output=True`` and the older-Airflow fallback,
# matching ``LLMOperator.execute``. ``LLMFileAnalysisOperator``
# overrides ``execute`` rather than calling ``super().execute()``,
# so the dump step has to be repeated here.
output = output.model_dump()
return output
def _build_system_prompt(self) -> str:
prompt = (
"You are a read-only file analysis assistant.\n"
"Use only the provided metadata, normalized file content, and multimodal attachments.\n"
"Do not claim to have modified files or executed any external actions.\n"
"If file content is truncated or sampled, say so in your answer."
)
if self.system_prompt:
prompt += f"\n\nAdditional instructions:\n{self.system_prompt}"
return prompt