Parsing from scratch
Parser turns a
Circular into structured data
according to an optional Configuration. Its parse method returns an
Output object wrapping the extracted
claims and providing methods to import and export parsed data.
Running the parser requires either setting the LLM_API_KEY environment
variable to your provider API key or passing the API key directly to Parser.
In this example, we assume LLM_API_KEY is already set and run the parsing
pipeline:
from tgcn import Circular, Configuration, Parser
debug = []
configuration = Configuration.load("config.json")
circular = Circular.from_web(44430)
parser = Parser(configuration=configuration)
output = parser.parse(circular, on_call=debug.append)
output
tgcn.Parser
Parse circulars using configured stage attempts.
Attributes:
| Name | Type | Description |
|---|---|---|
configuration |
Configuration
|
Connection and ordered settings for each stage. Defaults to the bundled configuration. |
api_key |
str | None
|
API key for all model calls. If omitted or blank, read the
key from |
Raises:
| Type | Description |
|---|---|
MissingAPIKeyError
|
No API key is available. |
parse
parse(circular: Circular, *, on_call: Callable[[Meta], None] | None = None) -> Output
Parse a circular into claims with normalized quantities.
If a stage exhausts its configured retries, the last error propagates with earlier failures chained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circular
|
Circular
|
Source circular. |
required |
on_call
|
Callable[[Meta], None] | None
|
Optional callback that receives a summary after each model attempt. When it raises, exceptions propagates. |
None
|
Returns:
| Type | Description |
|---|---|
Output
|
The normalized claims, circular identity, versions, and call history. |
Raises:
| Type | Description |
|---|---|
ValueError
|
A required stage has no configured attempts, or a quantity has an invalid value or references an unknown segment. |
OverflowError
|
The publication timestamp is outside the range of
|
ModelOutputError
|
The model response is invalid. |
APIError
|
A model API request fails. |
SegmenterError
|
Annotations differ from the circular body or sentence spans are invalid. |
tgcn.llm.Meta
Metadata for one model call.
SDK retries share one record. Meta reports call request parameter as reported in configuration and should be considered unsafe for export.
completed_at
completed_at: AwareDatetime
A datetime ISO 8601 timestamp for when the call completed.
elapsed_seconds
elapsed_seconds: float
Elapsed seconds including SDK retries, waits, parsing, and validation.
error
error: ErrorInfo | None = None
Details of the failure, when available.
response_id
response_id: str | None
ID of the final response, when available.
settings
settings: RequestSettings
Request settings used for the call.
stage
stage: Stage
Parsing stage that made the call.
success
success: bool
Whether the response was parsed and processed successfully.
url
url: str
Provider URL used for the call.
usage
usage: TokenUsage | None
Token usage from the final response, when available.
version
version: str
Package version that executed the call.
version_parser
version_parser: str
Parser version that executed the call.
Writing a configuration file
A Configuration controls the provider connections, models, and request
parameters used by the parser. Each stage—lines, quantities, and
claims—has an ordered list of attempts. If an attempt fails, the parser
tries the next until one succeeds or none remain. You can load a
configuration from a JSON file or use the bundled default.
This is an example configuration JSON file:
{
"api_urls": {
"responses": "https://openrouter.ai/api/v1",
"decisions": "https://openrouter.ai/api/alpha/decisions"
},
"lines": [
{
"api": "decisions",
"model": "typesafe/jev-1.13",
"parameters": {}
}
],
"quantities": [
{
"api": "responses",
"model": "openai/gpt-6-luna",
"parameters": {
"max_output_tokens": 16384,
"reasoning": {
"effort": "medium"
}
}
},
{
"api": "responses",
"model": "openai/gpt-6.1-sol",
"parameters": {
"max_output_tokens": 16384,
"reasoning": {
"effort": "high"
}
}
}
],
"claims": [
{
"api": "responses",
"model": "openai/gpt-6-luna",
"parameters": {
"max_output_tokens": 16384,
"reasoning": {
"effort": "medium"
}
}
}
]
}
This configuration sends all requests through OpenRouter. Each attempt's
api selects a URL from api_urls: an API root for Responses, or a full
endpoint for Decisions. There are three stages:
lineslabels the circular's lines, such as text and tables. It callstypesafe/jev-1.13through the Decisions API.quantitiesextracts measurements, such as times and fluxes. It first triesopenai/gpt-6-lunawith medium reasoning effort. If that fails, it triesopenai/gpt-6.1-solwith high reasoning effort.claimsidentifies what the circular says about its subjects, usingopenai/gpt-6-lunawith medium reasoning effort with no retry fallback.
The Decisions API is supported only for lines. All stages can use a
Responses API.
The parameters field holds extra options sent to the provider. An empty
{} adds no options. In this example, max_output_tokens limits the
model's output and reasoning.effort sets its reasoning effort. Supported
options depend on the provider and model.
tgcn.Configuration
Provider connections and model settings for parsing circulars.
The parser tries each stage's settings in order. Use None for an
unconfigured stage.
api_urls
api_urls: dict[Api, HttpUrl]
Provider URLs keyed by API. Use the API root for Responses and the full endpoint URL for Decisions.
claims
claims: list[RequestSettings] | None = Field(default=None, min_length=1)
Attempts for extracting claims.
lines
lines: list[RequestSettings] | None = Field(default=None, min_length=1)
Attempts for annotating the circular's body lines.
quantities
quantities: list[RequestSettings] | None = Field(default=None, min_length=1)
Attempts for extracting quantities.
default
default() -> Self
Load the default configuration.
The defaults were tuned for high precision and recall and use models that can be expensive to run.
Returns:
| Type | Description |
|---|---|
Self
|
A fresh configuration instance. |
load
load(path: Path | str) -> Self
Load and validate a JSON configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Configuration file to read. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The validated configuration. |
Raises:
| Type | Description |
|---|---|
OSError
|
The file cannot be read. |
ValidationError
|
The file contains malformed JSON or invalid settings. |
tgcn.config.Api
Api = Literal['responses', 'decisions']
APIs supported by the parser.