Implementation:AI4Finance Foundation FinRobot LLM Narrative Agent run
| Knowledge Sources | |
|---|---|
| Domains | Finance, LLM_Agent, Pipeline_Step |
| Last Updated | 2026-10-19 00:00 GMT |
Overview
Pattern implementation for the LLM narrative pipeline step: a PydanticAI `Agent.run(prompt, deps=deps)` call, constrained by a `TextValidator` with a per-step validator function, that narrates deterministic compute results rather than producing numbers itself.
Description
The FinRobot pipeline runtime (`finrobot.engine.pipelines.base`) provides a default agent-based step executor that wraps PydanticAI's `Agent.run` and pipes its output through a `TextValidator`. This is the concrete realization of the LLM_Narrative_Step pattern — every "narrative output" / "situation overview" / "investment thesis" step in the valuation pipelines uses this same executor, distinguished only by its `skill_section`, `required_data`, and validator function.
The validator functions live in `finrobot.engine.pipelines.validators`:
- `validate_dcf_output` — DCF narrative must contain assumptions, results, sensitivity
- `validate_ddm_output` — DDM narrative structural gate
- `validate_has_fields` — enforces a set of required fields (IC memo situation/risk)
- `validate_is_non_empty` — rejects empty output (LBO, earnings outlook)
- `validate_has_comps_table` — comps narrative must contain a multiples table
The LLM never computes a number. All deterministic results (DCFResult, PeerComps, EarningsResult, etc.) are injected from `structured_context` into the prompt; the agent only narrates them.
Code Reference
Source Location
- Repository: FinRobot
- Pipeline runtime: `finrobot_desktop/finrobot/engine/pipelines/base.py`
- Validators: `finrobot_desktop/finrobot/engine/pipelines/validators.py`
- Example PipelineStep (DCF narrative): `finrobot_desktop/finrobot/engine/pipelines/dcf.py:L252-258`
Signature
# Abstract shape of the default agent-based step (realized by PipelineStep in base.py)
async def default_agent_step(
agent: Agent, deps: FinRobotDeps, prompt: str,
structured_context: dict, ticker: str,
) -> StepOutput:
"""Run agent on prompt, then validate narrative via TextValidator."""
# Validator function shape (validators.py)
def validate_dcf_output(text: str) -> None:
"""Raise ValidationError if DCF narrative lacks required fields."""
Import
from finrobot.engine.pipelines.base import PipelineStep, TextValidator
from finrobot.engine.pipelines.validators import (
validate_dcf_output, validate_ddm_output,
validate_has_fields, validate_is_non_empty, validate_has_comps_table,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| agent | pydantic_ai.Agent | Yes | Report/analysis agent (model + system prompt + skill section) |
| prompt | str | Yes | Assembled from structured_context (deterministic results injected) |
| structured_context | dict | Yes | Holds the deterministic compute results for the prompt |
| deps | FinRobotDeps | Yes | Settings, data_layer, artifact_store |
| ticker | str | Yes | Target symbol |
Outputs
| Name | Type | Description |
|---|---|---|
| StepOutput | StepOutput | text narrative validated by TextValidator; persisted via build_*_artifact |
Usage Examples
DCF Narrative Step
# From dcf.py:L252-258 — the narrative step reuses the default agent executor
PipelineStep(
name="narrative",
executor=default_agent_step,
validator=TextValidator(validate_dcf_output),
required_data=[DataType.FINANCIALS],
)
# The DCFResult from the prior compute step is injected into the prompt
# from structured_context; the agent only narrates it.
Related Pages
Implements Principle
Requires Environment
Workflows Using This Step
- Workflow:AI4Finance_Foundation_FinRobot_DCF_Valuation (Step 3)
- Workflow:AI4Finance_Foundation_FinRobot_DDM_Valuation (Step 4)
- Workflow:AI4Finance_Foundation_FinRobot_LBO_Analysis (Step 4)
- Workflow:AI4Finance_Foundation_FinRobot_Comps_Analysis (Step 3)
- Workflow:AI4Finance_Foundation_FinRobot_IC_Memo (Steps 1, 3, 4)
- Workflow:AI4Finance_Foundation_FinRobot_Earnings_Analysis (Steps 2, 3, 4)
- Workflow:AI4Finance_Foundation_FinRobot_SEC_RAG_QA (Step 4)