Skip to content

ontocast.prompt.llm_json_schema

Format-bound JSON Schema generation for canonical LLM report models.

FormatBoundJsonSchemaGenerator

Bases: GenerateJsonSchema

Emit Turtle string or JSON-LD object schemas for RDF graph wire fields.

Source code in ontocast/prompt/llm_json_schema.py
class FormatBoundJsonSchemaGenerator(GenerateJsonSchema):
    """Emit Turtle string or JSON-LD object schemas for RDF graph wire fields."""

    def __init__(self, fmt: LLMGraphFormat, **kwargs: Any) -> None:
        super().__init__(**kwargs)
        self._fmt = fmt

    def generate(self, schema, mode="validation"):
        result = super().generate(schema, mode=mode)
        _patch_graph_field_schemas(result, self._fmt)
        return result

format_instructions_for_model(model, fmt)

LangChain-compatible format instructions using format-bound schema.

Source code in ontocast/prompt/llm_json_schema.py
def format_instructions_for_model(model: type[BaseModel], fmt: LLMGraphFormat) -> str:
    """LangChain-compatible format instructions using format-bound schema."""
    schema = schema_for_model(model, fmt)
    schema_str = json.dumps(schema, indent=2)
    return (
        "The output should be formatted as a JSON instance that conforms to the "
        "JSON schema below.\n\n"
        "As an example, for the schema "
        '{"properties": {"foo": {"title": "Foo", "description": "a list of strings", '
        '"type": "array", "items": {"type": "string"}}}, "required": ["foo"]}\n'
        'the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. '
        'The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.\n\n'
        "Here is the output schema:\n"
        f"```\n{schema_str}\n```"
    )

schema_for_model(model, fmt)

JSON Schema for model with graph fields locked to fmt wire encoding.

Source code in ontocast/prompt/llm_json_schema.py
def schema_for_model(model: type[BaseModel], fmt: LLMGraphFormat) -> dict[str, Any]:
    """JSON Schema for ``model`` with graph fields locked to ``fmt`` wire encoding."""
    return model.model_json_schema(schema_generator=_schema_generator_for(fmt))