Skip to content

Step 40: JSON Schema — schema-compiled constrained decoding #78

Closed

Description

@kkokosa

Summary

Implement JsonSchemaConstraint — schema-compiled constrained decoding that guarantees output conforms to a specific JSON Schema. Corresponds to response_format: {"type": "json_schema", "json_schema": {...}} in the OpenAI API.

Builds on Step 39 (JSON mode, #76) by layering a SchemaTracker on top of the existing JsonCharParser to enforce schema semantics during generation.

Acceptance Criteria

  • JsonSchemaConstraint : IDecodingConstraint enforces JSON Schema constraints via per-token vocabulary masking
  • Schema compiled into immutable CompiledSchema graph at request time
  • Property name enforcement via character trie (only valid property names allowed)
  • Type enforcement: restrict value types to schema-specified types
  • Required property enforcement: } blocked until all required properties emitted
  • additionalProperties: false support
  • Array items schema applied to each element
  • String enum and const value constraints
  • $ref / $defs resolution (non-recursive)
  • anyOf union type support
  • Integer type enforcement (no ./e/E in number generation)
  • ResponseFormat.JsonSchema variant added
  • TextGenerator dispatches to JsonSchemaConstraint for JsonSchema response format
  • CLI --response-format json_schema --schema <json|@file> support
  • Zero-alloc Clone() via struct-copy (SchemaTracker + JsonCharParser are value types)
  • Token mask caching with bounded cache (default 4096 entries)
  • Comprehensive unit tests for compiler, trie, tracker, and constraint

Supported Schema Features (MVP)

type, properties, required, additionalProperties, items, enum, const, $ref/$defs, anyOf, nested objects/arrays

Depends On

Key Files

  • src/DotLLM.Engine/Constraints/Schema/ (new directory)
  • src/DotLLM.Engine/Constraints/JsonSchemaConstraint.cs
  • src/DotLLM.Core/Configuration/ResponseFormat.cs
  • src/DotLLM.Engine/TextGenerator.cs

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions