dotFactory

Typed outputs for AI agents: let the words vary, never the shape

By Simone Mosciatti

How do you get structured output from an AI agent that the next step can trust? Declare the shape you want, ask the agent for exactly that shape, and check it when it comes back. Then let the next step read fields, not sentences.

A lot of multi-agent setups today are agents talking to each other in free text. I get the appeal. It's flexible, and the demo looks great.

But I don't think it holds up once something real depends on it. Not for anything that merges, deploys or asks a person.

The agent is usually fine. The trouble is the step after it, which has to guess what the agent meant.

Part of the Patterns for a software factory series.

You see it in small ways first.

A planner says "done" one day and "complete" the next. A reviewer forgets the severity on one finding. A field that is always there goes missing once in fifty runs. A "looks good, but maybe ask about the API first" gets read as a green light.

Each of these is a quiet bug, and quiet bugs are the expensive kind. The work looks fine until somebody checks.

Here is how I think about it. I can accept that what an agent writes isn't deterministic. The plan can be worded differently every time.

But the prompt, the files the agent can see and the steps around it should be known in advance. The handoff between two steps belongs on that list. It's the contract between them.

A typed handoff is just that contract, written down. Instead of "the planner returns some text", you say "the planner returns either a ready plan with steps and files, or a question for a person".

It has a declared type, like Plan. The agent is asked for that shape, not with a polite "please reply in JSON" but with a schema built from the type. The value is checked when it crosses the boundary. And the next step binds fields and guards on tags, so it never parses a paragraph.

In dotFactory you declare the out: ports of a step with a type. The OpenCode runner builds a schema from them and gives it to the agent with the prompt.

The types are small on purpose: six primitives (text, int, float, bool, time, secret), plus records, lists, maps, enum and choice.

A few rules do most of the work.

Records are closed, so a field the type doesn't declare is an error. There is no null: a field is present or absent, and only fields marked ? can be absent. A step doesn't fire with a half-filled record. And choice carries its tag, so a guard like when: plan.result is ready can branch on it.

The language guide has the rest, and my earlier post on how data moves through a Factory is the gentle version.

Isn't "reply in JSON" in the prompt enough? It helps, and I'd still do it. But it's a request, not a contract.

When it goes wrong you get a parse error, or worse, a missing field nobody notices. Model-level structured output is a good thing too, and it's part of how a runner can fill a schema. What a factory adds is that the type lives in the workflow: the same Plan is checked where it's produced and where it's used, and guards can route on it.

The file

Declare the handoff type, put it on the first step's out:, and bind it into the next step behind a guard. Here a planner hands a plan to an implementer, and either can say "not like this" in a way the file understands.

The trigger is a manual one to keep it short. In a real factory it would be an issue or a PR.

type Ticket = { key: text, title: text, body: text? }

type Plan = choice {
  ready   { summary: text, steps: [text], files: [text] }
  clarify { question: text }
}

type Change = choice {
  done    { branch: text, summary: text }
  blocked { reason: text }
}

external port credential(key: text) -> secret

runner agent:
  kind: opencode
  in:   open
  out:  open

trigger start:
  kind: manual
  out: { ticket: Ticket }

step plan:
  runner: agent
  in:
    token:  credential(key: "opencode")
    prompt: "Plan this ticket. If it is clear, return ready with a short summary, the steps, and the files you expect to touch. If something important is missing, return clarify with one question."
    ticket: start.ticket
  out: { result: Plan }

step implement:
  runner: agent
  when: plan.result is ready
  in:
    token:  credential(key: "opencode")
    prompt: "Implement the plan you are given, and nothing else. Return done with the branch you pushed, or blocked with the reason."
    ticket: start.ticket
    plan:   plan.result
  out: { result: Change }

The model token comes from credential.

"I need to ask" is a real answer. The clarify tag gives the agent a proper way to stop. Without it, agents tend to guess, because guessing is the only answer the format allows.

The implementer only runs on a ready plan. The when: guard reads the tag, so a question never turns into code. In this short version the question just sits on the Run. In yours, you'd post it back to the issue.

The plan arrives as data. Extra inputs to an OpenCode step are rendered to the model as data, so the implementer sees the steps and files as fields, not as a paraphrase.

And Change is the next handoff. A later step can open a PR from done, since the PR adapter needs a branch that is already pushed, and blocked can go to a person. Issue to pull request builds that out.

Where I'd skip it

Use typed handoffs whenever another step acts on an agent's answer: routing, merging, opening a PR, asking a person. That's most of a factory. Risk-based review routing and pull request triage are both typed handoffs underneath.

Skip the fancy types when the output only goes to a human, like a review comment or a summary. A plain text field is fine there.

And keep the types small. Three tags you actually branch on beat ten nobody reads. If a field never drives a decision, it probably doesn't belong in the type.

One more honest limit. A typed output makes the agent's answer well-formed, not correct. It can still return a ready plan that is wrong.

Types catch the "what shape is this?" bugs. That leaves your other checks, like CI, an adversarial reviewer or a human approval gate, free to spend their time on "is this right?".

Every value is also recorded on the Run, so when something odd happens you can see what the planner said and what the implementer got. The Runs guide shows where.

This is the same idea as always. The agent can say anything, and the file decides what happens next. I wrote about it in Put your software factory in a file.

If you want your agents to hand each other data you can trust, put the snippet in a .fct file and run it on dotFactory. The Getting started guide walks you through your first factory.

Frequently asked questions

What are typed outputs for AI agents?

The agent returns data in a declared shape, like a record with named fields or one of a few tags, instead of free text. The next step reads those fields directly, so a change in wording can't change what happens.

How do I pass data between AI agents reliably?

Declare the handoff as a type, have the producing step return it, and bind it into the next step. Check it at the boundary and branch on tags, never on phrases.

What happens if the agent returns the wrong shape?

In dotFactory, a value that doesn't match its type is a conformance error, and the next step doesn't fire with half a record. You see the failure on the Run instead of finding it downstream.

Do I need a special model for structured output?

No, dotFactory builds the schema from your out: ports and gives it to the agent. You can still pick the model or provider per step.

Portrait of Simone Mosciatti

Written by

Simone has spent more than 10 years building software, from research at CERN to big tech at AWS and fast-growing startups like Rippling. These days he's building dotFactory, because he thinks writing software should feel a lot more like running a well-oiled factory.