# `Imp.Signature`
[🔗](https://github.com/deepfates/imp/blob/v0.18.1/lib/imp/signature.ex#L1)

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"]

# `t`

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

# `dump`

Serializes a signature to JSON-friendly data.

# `ensure`

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

# `extend`

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`

Returns all field names, inputs first and outputs second.

# `input_names`

Returns input field names in declaration order.

# `json_schema`

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

# `load!`

Loads a signature produced by `dump/1`.

# `new`

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.

`instructions`, when given, replaces the spec's own: an existing signature
gets the new instructions, and a map's `:instructions` key is overridden.
DSPy's `ensure_signature` raises when given a signature and instructions;
Imp applies them, as it does for a map, so `Imp.signature/2` sets the
instructions of any spec.

# `output_names`

Returns output field names in declaration order.

# `prepend_output`

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`

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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
