Imp.Signature (Imp v0.5.0)

Copy Markdown View Source

Input/output contract for an Imp program.

A signature names the fields a program receives and the fields it must produce. It is the center of the Imp programming model: adapters render it for models, schemas validate structured outputs, optimizers mutate programs around it, and persistence stores it as plain data.

Required input presence is enforced before an LM call. To preserve DSPy's calling semantics, supplied input values that disagree with an explicitly declared type warn by default and still proceed. Output values are parsed and validated strictly. Validate untrusted application inputs at your own boundary when rejection is required.

Field names written in the string syntax are atoms only if that atom already exists, otherwise strings, so read values with Imp.get/2.

The compact string form is ideal for most code:

iex> signature = Imp.Signature.new("question: string -> answer: short_span")
iex> Imp.Signature.input_names(signature)
[:question]
iex> Imp.Signature.output_names(signature)
[:answer]
iex> Imp.Signature.json_schema(signature)["properties"]["answer"]["x-imp-answerShape"]
:short_span

Use the map form when constraints or metadata should be explicit data:

iex> signature =
...>   Imp.Signature.new(%{
...>     inputs: [:text],
...>     outputs: [
...>       %{name: :sentiment, type: :string, constraints: %{enum: ["positive", "negative"]}}
...>     ]
...>   })
iex> Imp.Signature.json_schema(signature)["properties"]["sentiment"]["enum"]
["positive", "negative"]

Summary

Functions

Serializes a signature to JSON-friendly data.

Returns a signature, constructing one when given a supported spec value.

Appends one or more fields to the input or output side of a signature.

Returns all field names, inputs first and outputs second.

Returns input field names in declaration order.

Exports output fields as a JSON-schema-shaped object.

Loads a signature produced by dump/1.

Builds a signature from a compact spec string, map, or existing signature.

Returns output field names in declaration order.

Prepends an output field.

Returns a compact inputs -> outputs display string.

Types

t()

@type t() :: %Imp.Signature{
  inputs: [Imp.Signature.Field.t()],
  instructions: String.t(),
  metadata: map(),
  outputs: [Imp.Signature.Field.t()]
}

Functions

dump(signature)

Serializes a signature to JSON-friendly data.

ensure(value)

Returns a signature, constructing one when given a supported spec value.

extend(signature, fields, kind)

Appends one or more fields to the input or output side of a signature.

fields accepts the same field shapes as Imp.Signature.Field.new/2.

field_names(signature)

Returns all field names, inputs first and outputs second.

input_names(signature)

Returns input field names in declaration order.

json_schema(signature)

Exports output fields as a JSON-schema-shaped object.

load!(state)

Loads a signature produced by dump/1.

new(spec, instructions \\ nil)

Builds a signature from a compact spec string, map, or existing signature.

Strings use the inputs -> outputs grammar and may include field types, descriptions, enums, and answer-shape aliases. Maps accept atom or string keys and are useful when signatures are loaded from JSON or built from structured configuration.

output_names(signature)

Returns output field names in declaration order.

prepend_output(signature, field)

Prepends an output field.

Chain-of-thought style modules use this to add a :reasoning field before the task outputs while preserving the original contract.

to_spec(signature)

Returns a compact inputs -> outputs display string.

This is intentionally a readable summary, not a lossless serialization. Use dump/1 when types, constraints, instructions, and metadata must round-trip.