Skip to content

API Reference parrot_tools.legal.librarian.flow

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.legal.librarian.flow

View source

The retrieval DAG (FEAT-449 §2 Overview flow, §3 M6).

query
  -> as_of_extract    (regex-first; structured LLM micro-call only for
                        genuinely ambiguous multi-date queries — R9)
  -> graph_retrieve    (explicit-id pass, then search_articles view
                        pattern — all inside the ArangoDB tenant, R14/R15)
  -> dossier_build     (PayloadEntry assembly, precedence order)
  -> LegalLibrarianAgent.draft()  (DraftAnswer; dossier span ids
                        enumerated in the prompt — R12)
  -> span_verify       (existence gate + anchor integrity + suppression
                        records — R4)
  -> ground             (GroundednessScorer atom check over the
                        surviving guide)

Every stage except the librarian LLM call is a deterministic callable — pure code, no LLM, no network (skeleton §5.3). answer() runs the six stages as direct, sequential async calls rather than through AgentCrew.run_flow's ToolNode template-placeholder plumbing: flow mode passes data between nodes via {input} / {nodes.<id>.output} string-templated placeholders resolved into a SINGLE tool call's args/kwargs, which does not fit this pipeline's fan-in shape (e.g. dossier_build needs BOTH graph_retrieve's hits AND as_of_extract's as_of AND the tenant store/ctx, none of which are plain strings) — see this task's Completion Note for the full reasoning. build_legal_librarian_crew still builds a structurally faithful AgentCrew (six registered nodes, ToolNode dependencies wired) as a discoverable, inspectable artifact; answer() is the executed/tested path.

Alias LegalAnswer

Re-exported from parrot_tools.legal.librarian.models.LegalAnswer.

Class LibrarianLike

Structural protocol satisfied by LegalLibrarianAgent and test doubles.

Method LibrarianLike.draft

draft(enumerated_dossier: str, query: str, as_of: date) -> Any

Parameter Type Default Description
enumerated_dossier str
query str
as_of date

Returns: Any

Method LibrarianLike.ask

ask(*args: Any, **kwargs: Any) -> Any

Parameter Type Default Description
*args Any ()
**kwargs Any {}

Returns: Any

Alias PayloadEntry

Re-exported from parrot_tools.legal.librarian.models.PayloadEntry.

Alias ReadingNote

Re-exported from parrot_tools.legal.librarian.models.ReadingNote.

Alias SpanRef

Re-exported from parrot_tools.legal.librarian.models.SpanRef.

Alias SpanVerifier

Re-exported from parrot_tools.legal.librarian.verifier.SpanVerifier.

Alias SuppressionLog

Re-exported from parrot_tools.legal.librarian.suppression.SuppressionLog.

Alias SuppressionRecord

Re-exported from parrot_tools.legal.librarian.models.SuppressionRecord.

Function answer

Run the retrieval DAG end-to-end for one query (§2 Overview flow).

Direct sequential invocation of the six stages — see this module's docstring for why AgentCrew.run_flow's ToolNode template plumbing was not used as the executed path.

answer(query: str, *, agent: LibrarianLike, store: Any, ctx: Any, log: SuppressionLog, user_id: str | None = None, materias: list[str] | None = None) -> LegalAnswer

Parameter Type Default Description
query str The user's query text.
agent LibrarianLike The librarian agent (LegalLibrarianAgent or a test double satisfying LibrarianLike).
store Any The tenant's graph store.
ctx Any Tenant context.
log SuppressionLog The append-only suppression log.
user_id str | None None User attributed to the execution, when known.
materias list[str] | None None Materias searched (single-materia corpus in v1 — defaults to empty; multi-materia routing is out of scope).

Returns: LegalAnswer — The final, span-verified, groundedness-checked LegalAnswer.

Alias article_in_force

Re-exported from parrot_tools.legal.boe.queries.article_in_force.

Function as_of_extract

Resolve the as_of date for a query (R9).

Regex-first: exactly one distinct date is used immediately. Zero distinct dates default directly to date.today() — no LLM call for the (overwhelmingly common) case of a query that doesn't mention a date at all. More than one distinct (genuinely ambiguous) date triggers the ONE structured LLM micro-call (extract_as_of).

as_of_extract(query: str, *, llm_ask) -> dict[str, Any]

Parameter Type Default Description
query str The user's query text.
llm_ask Injected async callable bound to the librarian's ask, used only for the genuinely-ambiguous multi-date fallback.

Returns: dict[str, Any] — {"query": query, "as_of": <resolved date>}.

Function build_legal_librarian_crew

Build a structurally faithful AgentCrew for the retrieval DAG.

Registers all six stages as crew members (five deterministic ToolNodes + the librarian LLM agent node) with their dependency edges wired, for inspection/discoverability. answer() — NOT this crew's run_flow — is the tested/executed path; see the module docstring for why.

build_legal_librarian_crew(agent: LibrarianLike, store: Any, ctx: Any, log: SuppressionLog) -> AgentCrew

Parameter Type Default Description
agent LibrarianLike The librarian agent.
store Any The tenant's graph store.
ctx Any Tenant context.
log SuppressionLog The append-only suppression log.

Returns: AgentCrew — The assembled AgentCrew (structural inspection only — see the Warning above; do not call run_flow() on it).

Function dossier_build

Assemble the retrieval set and its prompt enumeration.

dossier_build(hits: list[dict[str, Any]], as_of: date, *, limit: int = _DOSSIER_LIMIT) -> tuple[dict[str, PayloadEntry], str, dict[str, float | None]]

Parameter Type Default Description
hits list[dict[str, Any]] graph_retrieve output — explicit-id hits first, then BM25 hits.
as_of date The date resolved for this turn.
limit int _DOSSIER_LIMIT Maximum number of entries to include.

Returns: tuple[dict[str, PayloadEntry], str, dict[str, float | None]] — A tuple of: the retrieval_set (payload_key -> PayloadEntry, precedence-ordered), the prompt-formatted enumeration string, and a payload_key -> score map (None for explicit/traversal entries) used to order the final dossier.

Alias extract_as_of

Re-exported from parrot_tools.legal.librarian.as_of.extract_as_of.

Function graph_retrieve

Resolve retrieval candidates: explicit ids first, then BM25 search.

graph_retrieve(store: Any, ctx: Any, query: str, as_of: date) -> list[dict[str, Any]]

Parameter Type Default Description
store Any The tenant's graph store.
ctx Any Tenant context.
query str The user's query text.
as_of date The date to resolve in-force wordings for.

Returns: list[dict[str, Any]] — A list of hit dicts (articulo_key, norma_ref, numero, version, basis, score) — explicit-id hits first, then BM25 hits in the AQL's score order, both deduplicated by articulo_key.

Function ground

Suppress any reading-guide sentence whose atoms contradict the evidence.

Complementary check to span_verify — a CONTRADICTED numeric/ identifier atom in a (span-anchored) sentence means the LLM's narration around a valid citation misstated a fact; the citation (dossier span) remains valid, only the sentence is suppressed.

ground(answer: LegalAnswer, retrieval_set: dict[str, PayloadEntry], *, execution_id: str, user_id: str | None, log: SuppressionLog) -> LegalAnswer

Parameter Type Default Description
answer LegalAnswer The span-verified LegalAnswer (post span_verify).
retrieval_set dict[str, PayloadEntry] The turn's retrieval set (evidence source).
execution_id str The flow execution id, threaded into suppression records.
user_id str | None User attributed to the execution, when known.
log SuppressionLog The append-only suppression log.

Returns: LegalAnswer — answer with any atom-contradicted sentences removed from reading_guide and suppressed_count incremented accordingly.

Alias is_valid_boe_id

Re-exported from parrot_tools.legal.ids.is_valid_boe_id.

Alias regex_dates

Re-exported from parrot_tools.legal.librarian.as_of.regex_dates.

Alias search_articles

Re-exported from parrot_tools.legal.boe.queries.search_articles.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally