Schema System¶
Type: Implementation guide. Normative spec: PROTOCOL_SPEC §4 Schema Specification.
Overview¶
The Schema System loads, validates, resolves ($ref) and exports the input/output schemas of apcore modules. It bridges human-authored YAML schema files and the runtime schema objects the executor validates against, and it exports schemas to LLM tool-calling formats.
Requirements¶
- Load module schemas from YAML files and turn them into validated runtime representations.
- Resolve
$refreferences, including nested and cross-file references. A self-reference (a recursive data structure) is preserved as a lazy reference; only a$ref→$refchain that reaches no schema body is rejected as circular (PROTOCOL_SPEC §4.15). - Support the JSON Schema composition keywords (
oneOf,anyOf,allOf,not). Each SDK uses its idiomatic representation: Pydantic models in Python, TypeBox schemas in TypeScript,serde_json::Valuevalidated by thejsonschemacrate (Draft 2020-12) in Rust. - Validate data against a schema and report failures as structured errors (PROTOCOL_SPEC §4.14).
- Export schemas to the MCP, OpenAI, Anthropic and generic formats, including strict-mode export (§4.16).
- Recognise the LLM extension fields (
x-*) defined in §4.3. - Provide the three schema resolution strategies (§4.9).
- Cache resolved schemas by content hash so identical schemas are compiled once.
Components¶
SchemaLoader¶
SchemaLoader is the main entry point. It is constructed from a Config (reading schema.root, schema.strategy and schema.max_ref_depth), loads <schema.root>/<module/id/path>.schema.yaml for a module ID, resolves its $refs and builds the runtime schema objects. The strategy decides how YAML and code-defined (native) schemas interact:
yaml_first(default) — use the YAML file; fall back to the native schema if there is no YAML file.native_first— use the native schema; fall back to YAML if none is registered.yaml_only— use only YAML; raiseSCHEMA_NOT_FOUNDif the file is missing.
In practice the strategy is set with the schema.strategy config key rather than in code.
RefResolver¶
RefResolver resolves $ref references:
- Local references (
#/definitions/Foo,#/$defs/Foo). A local reference is resolved against the schema file's root first, then against the schema node being resolved (D-104). - Relative-file references (
other.schema.yaml#/definitions/Bar) and canonical references (apcore://...), resolved againstschema.root. - Cycle classification: re-entering a reference along a
$ref→$refchain raisesSCHEMA_CIRCULAR_REF; re-entering one after descending through a schema body is a self-reference and stays a lazy$ref. - A depth cap (
schema.max_ref_depth, default32); exceeding it raisesSCHEMA_MAX_DEPTH_EXCEEDED.
Resolved fragments are inlined into a copy of the schema; the input document is never mutated.
SchemaValidator¶
SchemaValidator validates data against a schema and returns a result object (valid, errors, plus an error code on failure) rather than raising. validate_input / validate_output are the raising variants the executor uses. See Contract: SchemaValidator.validate.
At the module-invocation boundary validation is strict: no type coercion is applied (TYPE_MAPPING §17.3). The validator's optional coerce_types flag is a library-level knob for callers validating their own untyped input, not something the executor enables.
SchemaExporter and ExportProfile¶
Core exports a provider-neutral representation of a module's schema and keeps
the JSON Schema extension fields. TypeScript also accepts ExportProfile.GENERIC
in SchemaExporter.export(). The MCP, OpenAI, and Anthropic profiles belong to
the adapter layer (§4.17).
use apcore::schema::{export_generic, SchemaDefinition};
use serde_json::json;
fn main() {
let definition = SchemaDefinition {
module_id: "common.echo".into(),
description: "Echo a message.".into(),
input_schema: json!({"type": "object"}),
output_schema: json!({"type": "object"}),
error_schema: None,
definitions: None,
version: None,
};
println!("{}", export_generic(&definition)["module_id"]);
}
Registry.export_schema(module_id, strict) exports a registered module's input and output schemas in the generic shape; pass strict=true to apply strict-mode conversion.
LLM extension fields¶
Schemas may carry the x-* fields defined in PROTOCOL_SPEC §4.3:
| Field | Purpose |
|---|---|
x-llm-description |
Description for LLM consumers; replaces description on export to AI formats |
x-examples |
Example values |
x-constraints |
Business rules JSON Schema keywords cannot express |
x-sensitive |
Marks a field as sensitive; its value is redacted in logs, traces and context.redacted_inputs |
Unknown x-* keywords are ignored during validation (forward compatibility).
Strict-mode export¶
to_strict_schema() (Python, Rust) / toStrictSchema() (TypeScript) converts a schema to the form OpenAI and Anthropic require under strict: true (PROTOCOL_SPEC §4.16, Algorithm A23): every object schema gets additionalProperties: false, every property becomes required (optional ones become nullable), and x-* fields and default values are removed. It is an export transform — it does not change how the executor validates input.
from apcore.schema import to_strict_schema
schema = {
"type": "object",
"properties": {
"to": {"type": "string", "x-examples": ["[email protected]"]},
"cc": {"type": "array", "items": {"type": "string"}, "default": []},
},
"required": ["to"],
}
strict = to_strict_schema(schema)
assert strict["additionalProperties"] is False
assert strict["required"] == ["cc", "to"]
assert strict["properties"]["cc"]["type"] == ["array", "null"]
import { toStrictSchema } from "apcore-js";
const schema = {
type: "object",
properties: {
to: { type: "string", "x-examples": ["[email protected]"] },
cc: { type: "array", items: { type: "string" }, default: [] },
},
required: ["to"],
};
const strict = toStrictSchema(schema);
console.log(strict.additionalProperties); // false
console.log(strict.required); // ["cc", "to"]
use apcore::to_strict_schema;
use serde_json::json;
fn main() {
let schema = json!({
"type": "object",
"properties": {
"to": { "type": "string", "x-examples": ["[email protected]"] },
"cc": { "type": "array", "items": { "type": "string" }, "default": [] }
},
"required": ["to"]
});
let strict = to_strict_schema(&schema);
assert_eq!(strict["additionalProperties"], json!(false));
assert_eq!(strict["required"], json!(["cc", "to"]));
}
Data flow¶
SchemaLoader.load(module_id)reads<schema.root>/<module/id/path>.schema.yamlinto aSchemaDefinition.RefResolverwalks the definition, inlining$reftargets (self-references stay lazy) and rejecting$ref→$refcycles.- The resolved schema is converted to the SDK's runtime representation (Pydantic model / TypeBox schema / compiled
jsonschemavalidator). - The result is cached by content hash and handed to the executor for validation or to the exporter.
Validation semantics¶
Union keywords¶
All branches of anyOf / oneOf are evaluated:
anyOfaccepts the input when at least one branch matches; otherwiseSCHEMA_UNION_NO_MATCH.oneOfaccepts the input when exactly one branch matches; zero matches isSCHEMA_UNION_NO_MATCH, more than one isSCHEMA_UNION_AMBIGUOUS.
These rules apply wherever the keyword appears — at the root, inside properties, items, $defs, or another combinator's branch — and on every validation path, including the executor's input and output validation steps. A host union type (typing.Union, TypeBox Type.Union) has anyOf semantics, so the SDKs enforce oneOf exclusivity themselves rather than relying on it. Authors must keep oneOf branches mutually exclusive.
Recursive schema support¶
A schema may refer to itself. When a $ref points to the schema's own $id, to the document root (#, #/), or to a location already on the resolution path that was reached through a schema body, the $ref is preserved rather than inlined again, and it does not raise SCHEMA_CIRCULAR_REF — that code is reserved for a $ref → $ref chain that never reaches a schema body (PROTOCOL_SPEC §4.15).
The preserved reference is bound lazily against the document root at validation time, so recursive positions are validated, not widened to "accept anything". These three forms are all self-references and behave identically:
| Form | Meaning |
|---|---|
{"$ref": "#"} |
the document being resolved |
{"$ref": "<root $id>"} |
the document, named by its own identifier |
{"$ref": "#/$defs/Node"} re-entered from inside $defs/Node |
a recursive $defs entry |
The recursive schema used across the conformance fixtures:
{
"$id": "TreeNode",
"type": "object",
"properties": {
"value": { "type": "string" },
"children": {
"type": "array",
"items": { "$ref": "TreeNode" }
}
},
"required": ["value"]
}
Constraint keywords¶
All three SDKs enforce the numeric and string constraints (minimum, maximum, exclusiveMinimum, minLength, maxLength, pattern), the array and object keywords (minItems, maxItems, uniqueItems, prefixItems, minProperties, additionalProperties), const / enum, and the composition keywords (allOf, anyOf, oneOf, not). Cross-SDK keyword behaviour is pinned by schema_hardening_constraints.json and schema_keyword_parity.json in conformance/fixtures/.
format is an annotation¶
Under JSON Schema 2020-12's default format-annotation vocabulary, a value that does not satisfy its declared format does not fail validation. A field declared format: date-time accepts any string. See TYPE_MAPPING §11, which is normative for this keyword:
- Implementations recognise the formats listed in TYPE_MAPPING §11.1 and emit a warning when a value does not conform (SHOULD).
- The value is accepted regardless; a format check never produces
SCHEMA_VALIDATION_ERROR. - An unrecognised
formatpasses silently.
To make a format binding, express it as an assertion: pattern for a syntactic shape, enum for a closed set.
Content-hash cache¶
Resolved schemas are cached in two levels: module ID → content hash, and content hash → compiled schema. The hash is the lowercase SHA-256 of the canonical JSON form of the resolved schema (object keys sorted), so two schemas with the same content — even under different module IDs or with different key order — share one compiled object. See Contract: content_hash.
Usage¶
from apcore.config import Config
from apcore.schema import SchemaLoader, SchemaValidator
from apcore.schema.strict import to_strict_schema
# The loader reads schema.root / schema.strategy from the Config;
# load() takes a MODULE ID, not a file path.
loader = SchemaLoader(Config.load("apcore.yaml"))
schema_def = loader.load("executor.email.send_email")
# resolve() returns (input, output) ResolvedSchema pairs carrying the Pydantic model.
input_schema, _output_schema = loader.resolve(schema_def)
validator = SchemaValidator()
result = validator.validate({"to": "[email protected]", "subject": "Hello"}, input_schema.model)
if not result.valid:
print(f"Validation failed ({result.error_code}): {result.errors}")
print(to_strict_schema(schema_def.input_schema))
import { SchemaValidator, toStrictSchema } from "apcore-js";
import { Config } from "apcore-js/config";
import { SchemaLoader } from "apcore-js/full";
// The loader reads schema.root / schema.strategy from the Config;
// load() takes a MODULE ID and is synchronous.
const loader = new SchemaLoader(Config.load("apcore.yaml"));
const schemaDef = loader.load("executor.email.send_email");
// resolve() returns [input, output] ResolvedSchema pairs carrying the TypeBox schema.
const [inputSchema] = loader.resolve(schemaDef);
const validator = new SchemaValidator();
const result = validator.validate({ to: "[email protected]", subject: "Hello" }, inputSchema.schema);
if (!result.valid) {
console.error(`Validation failed (${result.errorCode}):`, result.errors);
}
console.log(toStrictSchema(schemaDef.inputSchema));
use apcore::errors::ModuleError;
use apcore::schema::{to_strict_schema, SchemaLoader, SchemaValidator};
use apcore::Config;
use serde_json::json;
fn main() -> Result<(), ModuleError> {
// with_config() reads schema.root / schema.strategy; load() takes a MODULE ID.
let config = Config::from_defaults();
let mut loader = SchemaLoader::with_config(&config, None);
let schema_def = loader.load("executor.email.send_email")?;
// validate(value, schema) returns a ValidationResult; it does not raise.
let validator = SchemaValidator::new();
let result = validator.validate(
&json!({ "to": "[email protected]", "subject": "Hello" }),
&schema_def.input_schema,
);
if !result.valid {
eprintln!("Validation failed: {:?}", result.errors);
}
println!("{}", to_strict_schema(&schema_def.input_schema));
Ok(())
}
The Registry exports schema and description data independently of the publication protocol. Use Surfaces MCP converters for MCP, OpenAI, and Anthropic tool definitions.
Dependencies¶
- The Executor validates inputs at pipeline step 7 (
input_validation) and outputs at step 9 (output_validation) using the Schema System. - The Registry uses it to load module schemas during discovery and to build
ModuleDescriptors.
Python SDK reference
Not a protocol requirement — the source layout of apcore-python's schema package.
| File | Purpose |
|---|---|
schema/loader.py |
SchemaLoader: YAML loading, strategies, Pydantic model generation, content-hash cache |
schema/ref_resolver.py |
RefResolver: $ref resolution, self-reference vs. circular classification |
schema/validator.py |
SchemaValidator: result-object validation and the raising validate_input / validate_output |
schema/hardening.py |
content_hash, exhaustive union validation, format warnings |
schema/exporter.py |
Generic schema export |
schema/strict.py |
to_strict_schema (Algorithm A23) |
schema/openai_strict.py |
OpenAI strict-mode compatibility detection |
schema/annotations.py |
Merging YAML and code annotations, examples and metadata |
schema/types.py |
SchemaDefinition, ResolvedSchema, SchemaValidationResult, enums |
Runtime dependencies: pydantic (model generation), pyyaml (YAML parsing), jsonschema (exhaustive union validation).
Testing strategy¶
- Loader — YAML parsing, the three strategies, cache reuse.
- RefResolver — local, cross-file and nested references; a
$refre-entered throughproperties/itemssurvives as a lazy reference; a$ref→$refcycle raisesSCHEMA_CIRCULAR_REF; chains reachingschema.max_ref_depthraiseSCHEMA_MAX_DEPTH_EXCEEDED. - Validator — every supported type and keyword, both union error codes,
formatwarnings that do not fail validation. - Exporter — each profile's output and its handling of
x-*fields; strict-mode conversion output. - Model generation — constraints (required, types, patterns, enums) are enforced and
x-sensitivereaches redaction.
Cross-language fixtures in conformance/fixtures/: schema_validation.json, schema_keyword_parity.json, schema_hardening_union.json, schema_hardening_recursive.json, schema_hardening_constraints.json, schema_hardening_formats.json, schema_hardening_cache.json, schema_content_hash.json, schema_strict_conversion.json, schema_export_envelope.json.
Contract: SchemaValidator.validate¶
Inputs¶
data(dict/object/Value, required) — the data to validateschema(required) — the runtime schema: a Pydantic model class (Python), a TypeBoxTSchema(TypeScript), a JSON SchemaValue(Rust)
Errors¶
- None —
validatedoes not raise; failure is reported in the returned result object. For the raising form usevalidate_input/validate_output(all SDKs) orvalidate_or_error(Rust), which raiseSchemaValidationError(SCHEMA_VALIDATION_ERROR, or aSCHEMA_UNION_*code).
Returns¶
- Success:
SchemaValidationResult { valid: true, errors: [] }(Python, TypeScript) /ValidationResult { valid: true, errors: [] }(Rust) - Failure: the same shape with
valid: false, anerrorsarray of §4.14 detail objects (path,message,constraint,expected,actual), and an error code (error_code/errorCode) in Python and TypeScript.
Properties¶
- async: false
- thread_safe: true
- pure: true
- idempotent: true
Contract: RefResolver — $ref resolution¶
SDK surfaces¶
| SDK | Construction | Whole-document resolution | Single reference |
|---|---|---|---|
| Python | RefResolver(schemas_dir, max_depth=32) |
resolve(schema, current_file=None) |
resolve_ref(ref_string, current_file, ...) |
| TypeScript | new RefResolver(schemasDir, maxDepth = 32) |
resolve(schema, currentFile?) |
resolveRef(refString, currentFile, ...) |
| Rust | RefResolver::new() / RefResolver::with_max_depth(n), then .with_schemas_dir(dir) / .with_current_file(file) |
resolve(&schema) |
— |
SchemaLoader builds its resolver from schema.root and schema.max_ref_depth; most callers never construct one directly.
Inputs¶
schema(object/map/Value, required) — the schema document; every$refin it is resolved in one traversal. The input is not mutated.current_file/currentFile(path, optional) — the file the document was loaded from; relative references resolve against it and local#/…references resolve against its root first.schemas_dir/schemasDir/with_schemas_dir— root for relative-file andapcore://references.max_depth/maxDepth(integer, default32) — reference-depth cap.
Errors¶
SchemaCircularRefError(SCHEMA_CIRCULAR_REF) — a$ref→$refcycle. A self-reference is not an error.SchemaNotFoundError(SCHEMA_NOT_FOUND) — a referenced schema or pointer cannot be resolved.SchemaMaxDepthExceededError(SCHEMA_MAX_DEPTH_EXCEEDED) — the reference depth exceededmax_depth.SchemaParseError(SCHEMA_PARSE_ERROR) — a referenced file is not valid YAML/JSON.
Returns¶
- A copy of the schema with every
$refinlined, except self-references, which remain lazy$refnodes.
Properties¶
- async: false
- thread_safe: true
- pure: true (reads referenced files; caches them)
- idempotent: true
Contract: content_hash¶
content_hash (Python apcore.schema.hardening, Rust apcore::schema) / contentHash (TypeScript; contentHashAsync in the browser build).
Inputs¶
schema(dict/object/Value, required) — a resolved JSON Schema
Errors¶
- None.
Returns¶
str/string/String— the lowercase hexadecimal SHA-256 (64 characters) of the canonical JSON serialization ofschema(object keys sorted, no insignificant whitespace). The three SDKs produce byte-identical hashes;conformance/fixtures/schema_content_hash.jsonpins the canonical form for floats, non-ASCII text and large integers.
Properties¶
- async: false (
contentHashAsyncis async) - thread_safe: true
- pure: true
- idempotent: true