Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AI4Finance Foundation FinRobot LLM Narrative Step

From Leeroopedia
Knowledge Sources
Domains Finance, LLM_Agent, Pipeline_Step
Last Updated 2026-10-18 00:00 GMT

Overview

A pipeline-step pattern where a PydanticAI Agent generates a narrative explanation of deterministic compute results, constrained by a text validator that enforces structural requirements (non-empty, required fields, domain-specific validation) on the LLM output.

Description

The LLM Narrative Step is the FinRobot implementation of the core architectural principle: models reason, software computes, agents orchestrate, systems verify. The LLM never computes a number — it narrates the deterministic results produced by the compute operators.

The pattern decomposes into:

  1. Structured context injection: The deterministic compute results (DCFResult, PeerComps, EarningsResult, etc.) are injected into the LLM prompt from structured_context.
  2. Agent run: A PydanticAI Agent.run(prompt, deps=deps) call generates the narrative text. The agent may have a system prompt (e.g. "senior financial analyst") and a skill section (e.g. "initiating-coverage").
  3. Text validation: A TextValidator with a per-step validator function (e.g. validate_dcf_output, validate_has_fields, validate_is_non_empty) enforces structural requirements on the LLM output. If the output fails validation, the step may retry or degrade.
  4. Artifact building: The narrative text + structured data are passed to a build_*_artifact function that persists the full report chapter.

The validators are domain-specific:

  • validate_dcf_output: Enforces the DCF narrative contains required fields (assumptions, results, sensitivity)
  • validate_ddm_output: Same for DDM
  • validate_has_fields: Enforces a set of required fields (used by IC memo situation/risk)
  • validate_is_non_empty: Rejects empty output
  • validate_has_comps_table: Enforces the comps narrative contains a multiples table

Usage

Use this principle for every pipeline step where the LLM produces a narrative chapter:

  • DCF / DDM / LBO / Comps workflows (narrative step): The LLM explains the deterministic valuation results.
  • Equity_Research workflow (Steps 2, 7): Catalyst analysis and thesis synthesis.
  • IC_Memo workflow (Steps 1, 3, 4): Situation overview, investment thesis, risk factors.
  • Earnings_Analysis workflow (Steps 2, 3, 4): Financial context, earnings analysis, forward outlook.
  • SEC_RAG_QA workflow (Step 4): LLM answer with source citations.

Theoretical Basis

The separation principle:

Failed to parse (syntax error): {\displaystyle \text{Narrative} = \text{LLM}(\text{prompt}(\text{StructuredData}), \text{system\_prompt}) }

Failed to parse (syntax error): {\displaystyle \text{Validated} = \text{TextValidator}(\text{Narrative}, \text{validator\_fn}) }

Failed to parse (syntax error): {\displaystyle \text{Artifact} = \text{build\_artifact}(\text{Validated}, \text{StructuredData}) }

The LLM is the narrator; the validator is the gatekeeper; the artifact is the deliverable.

Pseudo-code:

# Abstract algorithm (NOT real implementation)
step = PipelineStep(
    executor=default_agent_step,  # Agent.run(prompt, deps=deps)
    validator=TextValidator(validate_dcf_output),
    required_data=[DataType.FINANCIALS],
)
result = await step.execute(agent, deps, prompt, structured_context, ticker)
artifact = build_dcf_artifact(result, ticker, deps)

Practical Guide

When implementing an LLM narrative step:

  1. Put all deterministic numbers in structured_context — never let the LLM compute them.
  2. Use a domain-specific TextValidator — non-empty is not enough; the narrative must contain the required structural fields.
  3. Use a skill section for domain context (e.g. "initiating-coverage", "comps-analysis").
  4. Wrap all external/untrusted content in <untrusted_*> blocks via sanitize_untrusted_block.
  5. The build_*_artifact function persists BOTH the narrative and the structured data — the narrative is never the source of truth for numbers.

Related Pages

Implemented By

This principle is a pipeline-step pattern documented via the PydanticAI `Agent.run` API + `TextValidator` runtime (`finrobot.engine.pipelines.base` + `validators.py`). It is realized across multiple workflows; the implementation page above documents the shared executor + validator set.

See the workflow pages for concrete realizations:

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment