ALLM.JsonSchema (allm v0.4.0)

Copy Markdown View Source

Shared helpers for JSON-Schema maps consumed by adapters.

Adapters expect string-keyed JSON Schema with string values for type, format, etc. Atom keys / atom values produce non-deterministic wire shapes across providers; normalize/1 enforces a single canonical form for both ALLM.Tool :schema AND ALLM.json_schema/3 :schema.

The normalization is idempotent: a map whose keys are all binaries AND contains no atom values passes through verbatim (fast path); otherwise the tree is walked and both keys and atom values are stringified.

Summary

Functions

Normalize a JSON-Schema map's atom keys AND atom values to strings.

Functions

normalize(schema)

@spec normalize(term()) :: term()

Normalize a JSON-Schema map's atom keys AND atom values to strings.

Returns the input verbatim (fast path) when keys are already binaries AND no atom values are present. Otherwise walks the tree recursively, rewriting atom keys via Atom.to_string/1 and atom values (other than nil, true, false) the same way.

Non-map inputs pass through unchanged — the helper is defensive against hydration paths that may produce nil or other values where a map is expected.

Examples

iex> ALLM.JsonSchema.normalize(%{type: :object, properties: %{name: %{type: :string}}})
%{"properties" => %{"name" => %{"type" => "string"}}, "type" => "object"}

iex> ALLM.JsonSchema.normalize(%{"type" => "object"})
%{"type" => "object"}