ontocast.integrations.serialize¶
Render OntoCast return values as text an LLM can read.
Most of the interesting return types -- :class:~ontocast.onto.rdfgraph.RDFGraph,
:class:~ontocast.onto.ontology.Ontology, DoclingDocument -- are not JSON
serializable, so every wrapped tool needs an explicit projection to text. These
helpers provide it, and enforce one rule the naive version gets wrong:
truncation must be announced. Cutting Turtle at a byte offset produces text that
looks like a complete graph but parses as a syntax error, and a model given
silently-truncated context will confidently reason about triples that were
dropped.
graph_to_llm_text(graph, *, max_chars, sources=None)
¶
Serialize a graph to Turtle for an LLM, with an explicit truncation marker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
The graph to render. |
required |
max_chars
|
int
|
Maximum characters to emit before truncating. |
required |
sources
|
Sequence[str] | None
|
Optional source IRIs, emitted as a leading comment so the model can cite where the triples came from. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Canonical Turtle, prefixed by a |
str
|
given and suffixed by a truncation note when the budget is exceeded. |
Source code in ontocast/integrations/serialize.py
json_to_llm_text(payload, *, max_chars)
¶
Render a JSON-serializable payload, truncating with a marker if needed.
Source code in ontocast/integrations/serialize.py
models_to_llm_text(items, *, max_chars, fields=None)
¶
Render pydantic results as a JSON list, dropping whole items when over budget.
Truncating by item rather than by character keeps the output valid JSON, which matters more here than showing a partial final record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[BaseModel]
|
The models to render. |
required |
max_chars
|
int
|
Character budget for the rendered list. |
required |
fields
|
Sequence[str] | None
|
Optional field allowlist; other fields are omitted. Use this to keep embedding vectors and other model-illegible payloads out. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A JSON array, with a trailing note when items were dropped. |
Source code in ontocast/integrations/serialize.py
truncate(text, *, max_chars)
¶
Cut plain text to a budget, announcing the cut.