teff.node.extract¶
teff.node.extract
¶
Extract — declarative structured-extraction recipe.
An :class:Extract is :class:~teff.node.ask.Ask's sibling: instead of
deciding pass/fail on an interrupt answer it extracts a structured object
from the conversation. It bundles the LLM extraction pass (a plain
:class:~teff.node.LLM with json_schema / output_type and optional
messages_key) with deterministic fallbacks that fill fields the model
left empty — a common failure mode of small local models.
The recipe is executed by the nodes :meth:Extract.nodes builds: the
extractor LLM first, then one :class:Fallback node per fallback.
Fallback is also usable standalone.
Classes:
| Name | Description |
|---|---|
Extract |
Declarative structured-extraction recipe ( |
Fallback |
Deterministic fallback that fills a field the model left empty. |
Extract
¶
Declarative structured-extraction recipe (Ask's sibling).
Builds [LLM extractor, *Fallback nodes] from a single spec — the
extraction half of a done chain::
extractor = Extract.model(
system="You extract project data...",
schema=PROJECT_INFO_SCHEMA,
model="llama3.1:8b",
provider="ollama",
messages_key="messages",
output_key="project_info",
fallbacks=[
Extract.fallback("room_type", room_from_first_user),
],
)
flow.interrupt_loop(key="approved", ..., done=extractor.nodes())
Use :meth:model to configure the LLM pass (equivalent to a plain
LLM with json_schema) and :meth:fallback to declare a
deterministic fill for a field the model may drop. Everything else is
threaded through to :class:~teff.node.LLM.
Methods:
| Name | Description |
|---|---|
fallback |
Declare a deterministic fill for field via |
llm |
Build the extraction |
model |
Build an extraction recipe around a structured |
nodes |
Build |
Source code in teff/node/extract.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
fallback
classmethod
¶
fallback(field, fn)
Declare a deterministic fill for field via fn(state).
fn receives the whole workflow state and returns the field value
(or None to skip). Runs after the LLM pass, only when the
model left field empty.
Source code in teff/node/extract.py
110 111 112 113 114 115 116 117 118 | |
llm
¶
llm()
Build the extraction LLM node.
Source code in teff/node/extract.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
model
classmethod
¶
model(*, system, schema, model, provider, **kwargs)
Build an extraction recipe around a structured LLM pass.
id (optional) names the built nodes in the compiled graph: the
extractor LLM becomes <id> and each fallback
<id>-fallback-<n>, so the topology shows extractor instead of
an auto-generated llm_chat_7.
Source code in teff/node/extract.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
nodes
¶
nodes()
Build [LLM extractor, *Fallback nodes] for flow wiring.
Source code in teff/node/extract.py
137 138 139 140 141 142 143 144 145 146 147 148 149 | |
Fallback
¶
Bases: Node
Deterministic fallback that fills a field the model left empty.
Reads a dict from input_key; when field in it is empty / None,
calls fn(state) and merges the returned value under field. No-op
when the dict already has the field or fn returns None.
Config
input_key: State key holding the extracted dict.
field: Dict field to fill when empty.
fn: Callable fn(state) -> value | None.
Source code in teff/node/extract.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |