Skip to content

API Reference

This reference is generated automatically from docstrings via mkdocstrings. See the Quickstart for a narrative walkthrough.

Functions

Public Python API: sample -> template, records, ast, canonical/readable DSL.

DSLResult(ast=TemplateAST(), canonical='', readable='', recognizers=list(), reason='', ready=False) dataclass

Bases: Serializable

Result of compiling a template into ast/canonical/readable/recognizers.

Attributes:

Name Type Description
ast TemplateAST

The parsed template AST (an empty TemplateAST() on failure).

canonical str

The canonical (regex-expanded) TextFSM template.

readable str

The human-readable DSL form of the template.

recognizers List[str]

Regex patterns that detect this block of text.

reason str

Failure reason if ready is False; "" on success.

ready bool

True if compiling succeeded.

LLMResult(template='', records=list(), variables=dict(), handling=list(), reason='', ready=False) dataclass

Bases: Serializable

Result of asking an LLM to turn a sample into a template.

Attributes:

Name Type Description
template str

The LLM-authored TextFSM template ("" if generation failed).

records List[Dict[str, str]]

Records the LLM claims to have parsed from the sample.

variables Dict[str, str]

Per-variable explanations the LLM provided.

handling List[str]

Notes on how the LLM handled ambiguous or edge-case lines.

reason str

Failure reason if ready is False; "" on success.

ready bool

True if generation succeeded.

ValidationResult(data=None, args=None, kwargs=None, reason='', ready=False) dataclass

Bases: Serializable

Generic pass/fail validation outcome.

Attributes:

Name Type Description
data Optional[Any]

The value that was validated (e.g. the template string, for validate_template()).

args Optional[list]

Reserved for callers that validate against positional inputs; unused by validate_template().

kwargs Optional[dict]

Reserved for callers that validate against keyword inputs; unused by validate_template().

reason str

Failure reason if ready is False; "" on success.

ready bool

True if validation succeeded.

DeliveryOutput(mode, output='', passed=False, error='') dataclass

Formatted result of running the full generate + compile pipeline.

Attributes:

Name Type Description
mode DeliveryMode

The verbosity mode output was formatted for.

output str

The formatted text (or JSON, if as_json=True was passed).

passed bool

True if the pipeline succeeded.

error str

Failure detail if passed is False; "" on success.

TemplateAST(values=list(), states=list()) dataclass

Parsed representation of a TextFSM/DSL template.

Attributes:

Name Type Description
values List[ValueNode]

The Value declarations parsed from the template.

states List[StateNode]

The states (each with its rules) parsed from the template.

parse_to_dicts(template, sample)

Parse sample with a TextFSM template, returning rows as dicts.

Source code in textfsm_ai/core/utils/template.py
22
23
24
25
def parse_to_dicts(template: str, sample: str) -> List[Dict[str, str]]:
    """Parse `sample` with a TextFSM `template`, returning rows as dicts."""
    parser = _create_parser(template)
    return parser.ParseTextToDicts(sample)

parse_to_lists(template, sample)

Parse sample with a TextFSM template, returning rows of raw values.

Source code in textfsm_ai/core/utils/template.py
16
17
18
19
def parse_to_lists(template: str, sample: str) -> List[List[str]]:
    """Parse `sample` with a TextFSM `template`, returning rows of raw values."""
    parser = _create_parser(template)
    return parser.ParseText(sample)

validate_template(template)

Validate that a TextFSM template is non-empty and syntactically valid.

Source code in textfsm_ai/core/utils/template.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def validate_template(template: str) -> ValidationResult:
    """Validate that a TextFSM template is non-empty and syntactically valid."""

    text = template.strip()
    if not text:
        return ValidationResult(
            data=template,
            reason="template_empty",
            ready=False,
        )

    try:
        _create_parser(text)
        return ValidationResult(data=template, ready=True)
    except Exception as ex:
        return ValidationResult(
            data=template,
            reason=f"template_syntax_error: {type(ex).__name__}: {ex}",
            ready=False,
        )

generate(sample, provider, api_key, model, *, endpoint='', api_version='', region='', project='', compartment_id='', max_retries=1, **kwargs)

Ask an LLM to turn a sample into a template, parsed records, variable explanations, and handling notes. Never raises.

Source code in textfsm_ai/api.py
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
def generate(
    sample: str,
    provider: str,
    api_key: str,
    model: str,
    *,
    endpoint: str = "",
    api_version: str = "",
    region: str = "",
    project: str = "",
    compartment_id: str = "",
    max_retries: int = 1,
    **kwargs,
) -> LLMResult:
    """
    Ask an LLM to turn a sample into a template, parsed records, variable
    explanations, and handling notes. Never raises.
    """
    pipeline = GenerationController(
        provider_name=provider,
        api_key=api_key,
        model=model,
        endpoint=endpoint,
        api_version=api_version,
        region=region,
        project=project,
        compartment_id=compartment_id,
        max_retries=max_retries,
    ).run(sample, **kwargs)

    metadata = pipeline.last_stage.metadata if pipeline.last_stage else None

    return LLMResult(
        template=metadata.template if metadata else "",
        records=metadata.records if metadata else [],
        variables=metadata.variables if metadata else {},
        handling=metadata.handling if metadata else [],
        reason=pipeline.reason,
        ready=pipeline.ready,
    )

to_llm_result(sample, provider, api_key, model, *, endpoint='', api_version='', region='', project='', compartment_id='', max_retries=1, **kwargs)

Alias of generate() — the full LLM result object.

Source code in textfsm_ai/api.py
 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
def to_llm_result(
    sample: str,
    provider: str,
    api_key: str,
    model: str,
    *,
    endpoint: str = "",
    api_version: str = "",
    region: str = "",
    project: str = "",
    compartment_id: str = "",
    max_retries: int = 1,
    **kwargs,
) -> LLMResult:
    """Alias of generate() — the full LLM result object."""
    return generate(
        sample,
        provider,
        api_key,
        model,
        endpoint=endpoint,
        api_version=api_version,
        region=region,
        project=project,
        compartment_id=compartment_id,
        max_retries=max_retries,
        **kwargs,
    )

to_llm_template(sample, provider, api_key, model, *, endpoint='', api_version='', region='', project='', compartment_id='', max_retries=1, **kwargs)

LLM template string, or the failure reason if generation didn't succeed.

Source code in textfsm_ai/api.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def to_llm_template(
    sample: str,
    provider: str,
    api_key: str,
    model: str,
    *,
    endpoint: str = "",
    api_version: str = "",
    region: str = "",
    project: str = "",
    compartment_id: str = "",
    max_retries: int = 1,
    **kwargs,
) -> str:
    """LLM template string, or the failure reason if generation didn't succeed."""
    result = to_llm_result(
        sample,
        provider,
        api_key,
        model,
        endpoint=endpoint,
        api_version=api_version,
        region=region,
        project=project,
        compartment_id=compartment_id,
        max_retries=max_retries,
        **kwargs,
    )
    return result.template if result.ready else result.reason

to_llm_records(sample, provider, api_key, model, *, endpoint='', api_version='', region='', project='', compartment_id='', max_retries=1, **kwargs)

LLM-parsed records, or [] if generation didn't succeed.

Source code in textfsm_ai/api.py
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
def to_llm_records(
    sample: str,
    provider: str,
    api_key: str,
    model: str,
    *,
    endpoint: str = "",
    api_version: str = "",
    region: str = "",
    project: str = "",
    compartment_id: str = "",
    max_retries: int = 1,
    **kwargs,
) -> List[Dict[str, str]]:
    """LLM-parsed records, or [] if generation didn't succeed."""
    result = to_llm_result(
        sample,
        provider,
        api_key,
        model,
        endpoint=endpoint,
        api_version=api_version,
        region=region,
        project=project,
        compartment_id=compartment_id,
        max_retries=max_retries,
        **kwargs,
    )
    return result.records if result.ready else []

to_llm_variables(sample, provider, api_key, model, *, endpoint='', api_version='', region='', project='', compartment_id='', max_retries=1, **kwargs)

LLM variable explanations, or {} if generation didn't succeed.

Source code in textfsm_ai/api.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
def to_llm_variables(
    sample: str,
    provider: str,
    api_key: str,
    model: str,
    *,
    endpoint: str = "",
    api_version: str = "",
    region: str = "",
    project: str = "",
    compartment_id: str = "",
    max_retries: int = 1,
    **kwargs,
) -> Dict[str, str]:
    """LLM variable explanations, or {} if generation didn't succeed."""
    result = to_llm_result(
        sample,
        provider,
        api_key,
        model,
        endpoint=endpoint,
        api_version=api_version,
        region=region,
        project=project,
        compartment_id=compartment_id,
        max_retries=max_retries,
        **kwargs,
    )
    return result.variables if result.ready else {}

to_llm_handling(sample, provider, api_key, model, *, endpoint='', api_version='', region='', project='', compartment_id='', max_retries=1, **kwargs)

LLM handling notes, or [] if generation didn't succeed.

Source code in textfsm_ai/api.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def to_llm_handling(
    sample: str,
    provider: str,
    api_key: str,
    model: str,
    *,
    endpoint: str = "",
    api_version: str = "",
    region: str = "",
    project: str = "",
    compartment_id: str = "",
    max_retries: int = 1,
    **kwargs,
) -> List[str]:
    """LLM handling notes, or [] if generation didn't succeed."""
    result = to_llm_result(
        sample,
        provider,
        api_key,
        model,
        endpoint=endpoint,
        api_version=api_version,
        region=region,
        project=project,
        compartment_id=compartment_id,
        max_retries=max_retries,
        **kwargs,
    )
    return result.handling if result.ready else []

compile_dsl(template, records)

Compile a template into its ast, canonical template, readable DSL, and recognizers. Never raises.

Source code in textfsm_ai/api.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
def compile_dsl(template: str, records: List[Dict[str, str]]) -> DSLResult:
    """
    Compile a template into its ast, canonical template, readable DSL, and
    recognizers. Never raises.
    """
    result = dsl_engine.run(template, records)

    return DSLResult(
        ast=result.ast or TemplateAST(),
        canonical=result.canonical or "",
        readable=result.readable or "",
        recognizers=result.recognizers or [],
        reason=result.reason or "",
        ready=result.ready,
    )

to_dsl_result(template, records)

Alias of compile_dsl() — the full DSL result object.

Source code in textfsm_ai/api.py
275
276
277
def to_dsl_result(template: str, records: List[Dict[str, str]]) -> DSLResult:
    """Alias of compile_dsl() — the full DSL result object."""
    return compile_dsl(template, records)

to_ast(template, records)

AST, or an empty TemplateAST() if the template failed to compile.

Source code in textfsm_ai/api.py
280
281
282
283
def to_ast(template: str, records: List[Dict[str, str]]) -> TemplateAST:
    """AST, or an empty TemplateAST() if the template failed to compile."""
    result = to_dsl_result(template, records)
    return result.ast if result.ready else TemplateAST()

to_canonical(template, records)

Canonical TextFSM template string, or the failure reason if compiling failed.

Source code in textfsm_ai/api.py
286
287
288
289
def to_canonical(template: str, records: List[Dict[str, str]]) -> str:
    """Canonical TextFSM template string, or the failure reason if compiling failed."""
    result = to_dsl_result(template, records)
    return result.canonical if result.ready else result.reason

to_readable(template, records)

Human-readable DSL string, or the failure reason if compiling didn't succeed.

Source code in textfsm_ai/api.py
292
293
294
295
def to_readable(template: str, records: List[Dict[str, str]]) -> str:
    """Human-readable DSL string, or the failure reason if compiling didn't succeed."""
    result = to_dsl_result(template, records)
    return result.readable if result.ready else result.reason

to_recognizers(template, records)

Recognizer regex patterns, or [] if compiling didn't succeed.

Source code in textfsm_ai/api.py
298
299
300
301
def to_recognizers(template: str, records: List[Dict[str, str]]) -> List[str]:
    """Recognizer regex patterns, or [] if compiling didn't succeed."""
    result = to_dsl_result(template, records)
    return result.recognizers if result.ready else []

run_pipeline(sample, provider, api_key, model, *, endpoint='', api_version='', region='', project='', compartment_id='', mode='default', as_json=False, max_tries=1)

Full pipeline: sample -> template, records, ast, canonical, readable, recognizers — packaged per mode ("quiet"/"default"/"info"/"debug").

Never raises for a failed run: check .passed/.error on the result.

Source code in textfsm_ai/api.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
def run_pipeline(
    sample: str,
    provider: str,
    api_key: str,
    model: str,
    *,
    endpoint: str = "",
    api_version: str = "",
    region: str = "",
    project: str = "",
    compartment_id: str = "",
    mode: str = "default",
    as_json: bool = False,
    max_tries: int = 1,
) -> DeliveryOutput:
    """
    Full pipeline: sample -> template, records, ast, canonical, readable,
    recognizers — packaged per `mode` ("quiet"/"default"/"info"/"debug").

    Never raises for a failed run: check `.passed`/`.error` on the result.
    """
    return DeliveryController(
        provider_name=provider,
        api_key=api_key,
        model=model,
        endpoint=endpoint,
        api_version=api_version,
        region=region,
        project=project,
        compartment_id=compartment_id,
        max_tries=max_tries,
    ).run(sample, mode=mode, as_json=as_json)

Result types

Result types returned by the functions in :mod:textfsm_ai.api.

LLMResult dataclass

Bases: Serializable

Result of asking an LLM to turn a sample into a template.

Attributes:

Name Type Description
template str

The LLM-authored TextFSM template ("" if generation failed).

records List[Dict[str, str]]

Records the LLM claims to have parsed from the sample.

variables Dict[str, str]

Per-variable explanations the LLM provided.

handling List[str]

Notes on how the LLM handled ambiguous or edge-case lines.

reason str

Failure reason if ready is False; "" on success.

ready bool

True if generation succeeded.

Source code in textfsm_ai/api_models.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@dataclass
class LLMResult(Serializable):
    """Result of asking an LLM to turn a sample into a template.

    Attributes:
        template: The LLM-authored TextFSM template ("" if generation failed).
        records: Records the LLM claims to have parsed from the sample.
        variables: Per-variable explanations the LLM provided.
        handling: Notes on how the LLM handled ambiguous or edge-case lines.
        reason: Failure reason if `ready` is False; "" on success.
        ready: True if generation succeeded.
    """

    template: str = ""
    records: List[Dict[str, str]] = field(default_factory=list)
    variables: Dict[str, str] = field(default_factory=dict)
    handling: List[str] = field(default_factory=list)
    reason: str = ""
    ready: bool = False

DSLResult dataclass

Bases: Serializable

Result of compiling a template into ast/canonical/readable/recognizers.

Attributes:

Name Type Description
ast TemplateAST

The parsed template AST (an empty TemplateAST() on failure).

canonical str

The canonical (regex-expanded) TextFSM template.

readable str

The human-readable DSL form of the template.

recognizers List[str]

Regex patterns that detect this block of text.

reason str

Failure reason if ready is False; "" on success.

ready bool

True if compiling succeeded.

Source code in textfsm_ai/api_models.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@dataclass
class DSLResult(Serializable):
    """Result of compiling a template into ast/canonical/readable/recognizers.

    Attributes:
        ast: The parsed template AST (an empty `TemplateAST()` on failure).
        canonical: The canonical (regex-expanded) TextFSM template.
        readable: The human-readable DSL form of the template.
        recognizers: Regex patterns that detect this block of text.
        reason: Failure reason if `ready` is False; "" on success.
        ready: True if compiling succeeded.
    """

    ast: TemplateAST = field(default_factory=TemplateAST)
    canonical: str = ""
    readable: str = ""
    recognizers: List[str] = field(default_factory=list)
    reason: str = ""
    ready: bool = False