Repository navigation
Description
inputs, outputs, set —
no struct to peel open now,
just[:answer], plain
This proposal was written by Claude Code.
This is a proposal. It has no code yet, and it's here to settle the shape before anyone writes the migration.
Motivation
Right now you have to learn two Imp-specific data types before you can do the two most common things with the library, which are labeling a row and reading an answer.
Building a labeled row takes two steps, and the input/label split only appears in the second one:
Imp.example(question: "2+2?", answer: "4") |> Imp.with_inputs(:question)If you forget with_inputs/2, you still get a valid %Imp.Example{} whose input_keys is nil. Imp.Example.inputs/1 then hands the program every field, including the answer, and nothing raises. You only find out when the evaluation scores look too good.
Reading a result means going through Imp.get/2 or Imp.Prediction.get/2, because %Imp.Prediction{} keeps its outputs under :fields:
{:ok, prediction} = Imp.call(program, %{question: "..."})
Imp.get(prediction, :answer) # works
prediction[:answer] # raises: the struct doesn't implement Access
Map.get(prediction, :answer) # nil: the answer is at prediction.fields.answerNothing about a prediction needs a struct. It holds data, and it has no invariants a map couldn't carry. The struct just makes ordinary Elixir (Map.get, [:key], pattern matching, Enum over the outputs) either not work or quietly return the wrong thing.
Pattern matching and control flow
This is where a plain map makes the biggest difference. You can match a prediction today, but only by naming the struct and its internal :fields key (%Imp.Prediction{fields: %{kind: "bug"}}). The docs never show that, so every example reads fields with Imp.get/2 and then branches in a second, nested case:
case Imp.call(triage, %{issue: text}) do
{:ok, prediction} ->
case Imp.get(prediction, :kind) do
"bug" -> file_bug(Imp.get(prediction, :summary))
"feature" -> add_to_roadmap(Imp.get(prediction, :summary))
"question" -> reply(Imp.get(prediction, :summary))
end
{:error, reason} ->
{:error, reason}
endWith outputs as a plain map, a single case handles the result, the typed output, and the failure. Each clause pulls out exactly the fields it uses, and a ReActV2 turn that ended without an answer gets a clause of its own, where today you'd call Imp.Prediction.complete?/1:
case Imp.call(triage, %{issue: text}) do
{:ok, %{outputs: %{kind: "bug", summary: summary}}} -> file_bug(summary)
{:ok, %{outputs: %{kind: "feature", summary: summary}}} -> add_to_roadmap(summary)
{:ok, %{outputs: %{kind: "question", summary: summary}}} -> reply(summary)
{:ok, %{metadata: %{termination_reason: :incomplete}}} -> retry_later(text)
{:error, reason} -> {:error, reason}
endThe same shape works in with when you chain programs, so each step takes the field the next step needs, and anything that doesn't match drops out to else without an Imp.get/2 in between:
with {:ok, %{outputs: %{query: query}}} <- Imp.call(rewrite, %{question: question}),
{:ok, %{outputs: %{answer: answer}}} <- Imp.call(respond, %{question: question, query: query}) do
{:ok, answer}
endIt also works in function heads, which is how most Elixir code already dispatches. A metric or a GenServer callback can match the output it cares about right in its arguments, and a guard can check the rest:
def handle_info({:triaged, %{outputs: %{kind: "bug", severity: severity}}}, state)
when severity in ["high", "critical"] do
{:noreply, page_on_call(state)}
end
def score(example, %{outputs: %{answer: answer}}),
do: if(answer == Imp.Example.get(example, :answer), do: 1.0, else: 0.0)None of these ask the reader to learn an Imp function. They're ordinary Elixir, and because every program returns the same four keys, the clauses you write against one program keep working when you swap it for a chain-of-thought or ReAct version of the same signature.
Proposed API
Examples say which fields are inputs when you build them
Imp.example(inputs: [question: "2+2?"], outputs: [answer: "4"])
# or, equivalently
Imp.example(%{inputs: %{question: "2+2?"}, outputs: %{answer: "4"}})Imp.example/1 requires :inputs, and :outputs is optional (an unlabeled row is fine). A row can no longer exist without an input split, so the bug above goes away. Imp.with_inputs/2 is removed from the facade. The flat constructor stays on Imp.Example.new/1 for dataset loaders that split fields later.
We're keeping %Imp.Example{} as the internal representation for this step. It carries demos and imp_ internal fields, and every optimizer walks it, so turning it into a map is a separate decision. Only the constructor changes here. Imp.Example.to_map/1 still returns every field flat, inputs and outputs together, the same as it does today.
Every program returns a plain map
Imp.call/2, and more generally every Imp.Module.call/2 implementation, returns:
{:ok,
%{
outputs: %{answer: "Paris"},
metadata: %{trace: ..., lm_usage: ...},
completions: [],
score: nil
}}All four keys are always present, so %{outputs: outputs} = prediction always matches, and prediction.metadata.trace keeps working the way the livebooks use it today. Reading an answer is then just ordinary Elixir:
{:ok, %{outputs: %{answer: answer}}} = Imp.call(program, %{question: "..."})
prediction.outputs.answer
prediction[:outputs][:answer]
Map.get(prediction.outputs, :answer)Because the change covers the whole contract and not only the facade, metrics, optimizers, Imp.evaluate/3, and custom Imp.Module programs all get the same map back. A user never meets one shape from Imp.call/2 and a different one inside their metric function.
Changes
Imp.Predictionstruct is deleted. Its helpers either go away, where aMapfunction does the same job (get/3,fetch!/2,put/3,to_map/1), or become functions over the map (complete?/1,get_lm_usage/1, anew/2constructor that validates the four keys).%Imp.Prediction{}is currently matched in 34 files underlib/(68 sites) and mentioned about 390 times across 92 test files, and all of those move to%{outputs: _}matches or map access.Imp.Modulecallbackscall/2andexecute/3change their return type to the map.Imp.Module.call/2keeps normalizing malformed returns, and{:invalid_module_result, ...}now means "not a map with:outputs".Imp.get/3andImp.to_map/1lose their prediction clauses, and we should decide whether they're still worth keeping for examples alone.Imp.example/1takesinputs:/outputs:, andImp.with_inputs/2leaves the facade (Imp.Example.with_inputs/2stays for internal use).- Metrics: built-in metrics (
Imp.exact_match/1and the others) readprediction.outputs, so the signature users write metrics against changes. - Persistence: saved programs store demos as examples rather than predictions, so we don't expect a format change. The migration should confirm that no saved artifact serializes a
%Imp.Prediction{}. - Docs: README, cheatsheet,
coming-from-dspy.md(thedspy.Predictionrow), all five livebooks, and adecisions.mdrow recording the ruling. priv/public_api.jsongets regenerated.
Open questions
- String keys and
[:key]. A 2026-09-25 ruling indecisions.mdsays a key stays the atom or string it was given, and every lookup compares keys by their text, soImp.get(p, :answer)finds a field stored under"answer". PlainMap.get/2and[:key]don't do that. A program whose outputs come back with string keys (MCP-sourced or JSON-schema signatures) would makeprediction.outputs[:answer]returnnil. We can either say outputs are keyed exactly as the signature declares them, which means atoms for signatures written in Elixir and strings only when the signature itself used strings, or keep one text-comparing helper for the cases that need it. This question decides whether the ergonomic win actually holds. - A field named
inputs.Imp.example(inputs: ..., outputs: ...)takes over those two names at the top level. A dataset with a column literally calledinputswould have to go throughImp.Example.new/1. That seems acceptable, but we should write it down. - DSPy parity.
dspy.Predictiondoes support attribute and[]access to outputs directly (pred.answer). This proposal puts outputs one level down, under:outputs, so metadata can't collide with a field name.coming-from-dspy.mdshould say that plainly and not present it as a one-to-one mapping. - Examples as plain maps too. Doing the same thing to
%Imp.Example{}, as%{inputs:, outputs:}, would make both sides symmetric, so a metric could compareexample.outputs.answerwithprediction.outputs.answer. It's left out here because demos and internal fields would need somewhere to live first.
Rollout
This is a breaking change to 0.5.x's public surface, and it ships as 0.6.0 with no compatibility shim, following the pre-1.0 practice in decisions.md where adopting a new path deletes the old one in the same change.
🤖 Generated with Claude Code
Activity