karenina.benchmark.verification.utils.schema_builder¶
schema_builder
¶
Build JSON schemas for the judge LLM, filtering out verification metadata.
Two filtering operations: 1. Remove trace fields (judge should not parse these) 2. Strip verification metadata (judge must not see ground truth)
Also exposes build_extraction_relaxed_class which produces a sibling
Pydantic model with every VerifiedField coerced to Optional[T] = None.
This relaxed class is used at agentic-extraction time so that a single
null field from the LLM does not reject the entire record. The caller
recombines null-valued fields into the strict template afterwards via
BaseAnswer.model_construct plus a private _null_fields set.
Classes¶
Functions¶
build_extraction_relaxed_class
¶
build_extraction_relaxed_class(
answer_class: type[BaseAnswer],
) -> type[BaseAnswer]
Build a sibling Pydantic class with VerifiedFields relaxed to Optional.
Each VerifiedField on answer_class is rewritten as Optional[T] = None
so that the agentic parser accepts null for any single field without
failing the whole record. The relaxed class is used only at extraction
time; downstream verification then reconstructs the strict template and
tracks which fields came back null via a private _null_fields set.
Non-VerifiedField fields (e.g. id) and unverified plain fields are
preserved as-is; only fields that carry __verification__ metadata are
loosened, because only those participate in scoring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
type[BaseAnswer]
|
The strict template class to derive from. |
required |
Returns:
| Type | Description |
|---|---|
type[BaseAnswer]
|
A new Pydantic |
Source code in src/karenina/benchmark/verification/utils/schema_builder.py
build_parsing_schema
¶
build_parsing_schema(
answer_class: type[BaseAnswer],
) -> dict[str, Any]
Build JSON schema for the judge, filtering out trace fields and metadata.
Handles Pydantic v2 schema shapes including $defs and allOf references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
type[BaseAnswer]
|
The Answer class to generate a schema for. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Filtered JSON schema dict safe to send to the judge LLM. |
Source code in src/karenina/benchmark/verification/utils/schema_builder.py
rebuild_strict_answer_with_null_fields
¶
rebuild_strict_answer_with_null_fields(
answer_class: type[BaseAnswer],
relaxed_instance: Any,
unanswered_fields: set[str] | None = None,
) -> BaseAnswer
Rebuild a strict Answer while preserving null verified fields.
Parser adapters receive the relaxed class from
build_extraction_relaxed_class. This helper converts the parsed relaxed
instance back to the original strict answer class, omitting null field
values and storing the names of verified null fields on _null_fields.
BaseAnswer._compute_field_results consumes that marker and reports
those fields as None instead of conflating them with False.