Structured output7 min read
How to get checked JSON from a phone screen with an AI agent
A phone agent can return JSON that matches your schema and is still wrong. Here is how to send Lusor a schema, what comes back, and what Lusor does with a value the agent never read on the screen.
By Josiah Richards
TL;DR
- Add a JSON Schema as schema when you start a session on a phone. When the session is completed, output holds the agent's answer in that shape and outputSchema echoes the schema.
- Schema validation proves the shape, not the values. In one production run the screen said iOS 27.2.1, the agent answered 18.6.2, and the answer matched the schema.
- Before Lusor accepts a schema answer, it checks versions, codes with letters and digits, and possessive names against everything the run read. An unread one is refused, and the agent is told to read the field again or leave it out.
- After two refusals the answer is delivered as written, and the run's summary lists each value as "Couldn't confirm on screen". Lusor never rewrites output, so it always matches your schema.
- Prices, plain numbers, dates and plain words are not checked, and a value the check passes can still be the wrong one on the screen. Record the session when a value matters.
Code that stores what a phone agent found needs fields, not a paragraph. Lusor takes a JSON Schema with a session and returns the agent's answer in that shape. This post covers the request, the response, and the check Lusor runs on the answer before it accepts it, including what that check does not cover.
For getting app data into a spreadsheet instead, or exporting a run's results by hand, read how an agent copies app data into a Sheet.
Why is a model's JSON not the same as data?
Because a schema checks the shape of an answer, not where its values came from. Before Lusor accepts a schema answer, it validates it against your schema. An answer with a missing required field or a wrong type goes back to the agent with the error, and the agent tries again. So the JSON you receive always parses and always has the fields your schema requires.
That still leaves the values. In one production run on an iPhone 13 Pro Max, the agent was given a schema with one field, version. The only screen it read said "iOS Version, 27.2.1". The agent answered {"version": "18.6.2"}, a version it never saw. The answer matched the schema, so it was accepted and the session ended as completed.
A wrong value in well-formed JSON is worse than a missing one, because nothing downstream notices it. A parser accepts it, a database stores it, and a dashboard shows it. That run is why Lusor now checks the values too.
How do you ask a phone agent for a schema?
Start a session on the phone directly, without an agent, and add schema to the body of POST /v1/workspaces/{workspaceId}/devices/{deviceId}/sessions. The sessions without an agent docs list the other fields that call takes. Turns sent to an agent do not take a schema.
The docs' example asks for "the price of a 12-pack of sparkling water in the grocery app" with this schema: an object with a price number and a currency string, both required. The call returns 202 with the session's id and status: "queued". When the session is completed, GET /v1/sessions/{id} returns these fields.
| What it holds | |
|---|---|
| status | "completed" once the run has finished. Read output only then. |
| output | The agent's answer, as an object that follows your schema, with no wrapper around it. |
| outputSchema | The schema you sent, echoed back. |
| error | null on success. A failed session has an error here instead of an output. |
In the docs' example, output is {"price": 6.49, "currency": "USD"}. The same output is also in GET /v1/sessions/{id}/artifacts.
The structured output guide has three tips that matter for what follows. Describe the task in prompt and the shape in schema, because the schema does not replace instructions. Mark the fields you always need as required. Put a description on each property to say what it means, such as the currency format you want.
This post is about code that calls the API. To hand jobs to Lusor from Claude, ChatGPT or another AI app instead, read what Lusor's MCP server for real phones exposes.
What happens to a value the agent did not read on screen?
The agent finishes a schema session by calling done with its answer and its reasoning. Lusor walks the strings and numbers in the answer, names each one by its path, such as answer.version or answer.items[0].name, and checks it against everything the run read from the phone. The reasoning goes through the same check.
Three kinds of value are checked.
- Versions. A value such as
27.2.1. - Codes. A value that mixes letters and digits, such as a model number.
- Possessive names. A name written with 's, such as a device name.
When one of these appears in nothing the run read, Lusor refuses the answer. The agent gets back the field and the value, and an instruction to take the value from the screen by reading the field again and copying it exactly, or to leave it out. The refusal never names a value to use instead, so the agent cannot copy a fix from it.
Lusor refuses twice. If the third answer still holds an unread value, Lusor delivers it as written and adds a note to the end of the run's summary, for example Couldn't confirm on screen: "18.6.2". It never rewrites output, because a changed answer could stop matching your schema.
After this check shipped, the same request on the same iPhone returned 27.2.1. The check uses the reads the run already made and takes no screenshot of its own. A refusal does cost the run more steps, and on a phone each step includes reading the screen and waiting for the app, as where a phone agent's time goes explains.
What does a missing field or a flagged value tell your code?
Two things can happen to a value the agent could not read. Each one shows up in a different place.
- The field is missing. The refusal tells the agent it may leave an unread value out. It can only do that for a field your schema does not require. A missing optional field means the agent did not report that value, which is safer to store than a guess.
- The value is flagged. A required field has to hold something, so after two refusals an unread value can still arrive in
output. The flag is in the run's summary, which thesession_completedevent carries indata.summary. Read it fromGET /v1/sessions/{id}/events, which the follow a session docs cover. A summary that containsCouldn't confirm on screennames the values your code should treat as unverified.
So mark a field required only when the value is always on the screen the task reaches. For a field that some screens will not show, leave it optional, and treat a missing key as "not found".
What can the check not prove?
The check answers one question: did this value appear in something the run read? Several kinds of mistake fall outside it.
- Values it does not check. Plain numbers, prices and other amounts, units, sizes, dates, times, booleans and plain words pass without a check. Both fields of the docs' price example are of this kind.
- Values it lets through. A value you wrote in the prompt counts as read, and so does any value your schema lists in an
enumorconst, because the schema accepts no other. The check is off for a task that asks the agent to write or suggest something, such as a draft or a name to give, since that answer is not meant to be read off the screen. - The wrong value on the right screen. A value that passes was on the screen, but it can still be the wrong row or the wrong field. The check proves where the value came from, not that it answers your question.
- Correct values it flags. A value that appears only in a picture, not in the screen's text, is refused twice and then flagged even when it is right. So is a value the agent built rather than read, such as a URL it assembled.
For a value that matters, record the session. Set recording: true when you start it, and the recordings and replay docs show how to fetch the video and the step-by-step screenshots. Checking a flagged value then takes one look at the screen it came from, the approach in how to check an agent that says it is done. What the agent can and cannot see on a phone in the first place is covered in whether it is safe to let an agent use your phone.
To judge the agent itself across more than one run, read what a mobile agent benchmark score leaves out.
Frequently asked questions
How do you get JSON from a phone agent with Lusor?
Start a session on the phone without an agent and add a JSON Schema as schema in the request body. When the session's status is completed, GET /v1/sessions/{id} returns the agent's answer in output, in the shape the schema describes. Turns sent to an agent do not take a schema.
Is schema-valid JSON from an agent correct?
Not necessarily. Validation checks that the answer has the right fields and types, not that the values came off the screen. In one production run the only screen the agent read said iOS 27.2.1, the agent answered 18.6.2, and the answer passed the schema.
What does Lusor do when the agent reports a value it never read?
It refuses the answer and tells the agent which field holds a value that appears in nothing the run read, without naming a value to use instead. The agent has to read the field again and copy it, or leave it out. After two refusals the answer is delivered as written, and the run's summary ends with Couldn't confirm on screen and the values.
Which values does Lusor check against the screen?
Versions such as 27.2.1, codes that mix letters and digits, and possessive names, in every string and number of the answer and in the agent's reasoning. Plain numbers, prices and other amounts, units, dates, times and plain words are not checked. Neither are values you gave in the prompt or values your schema lists in an enum or const.
How does your code know a value was not confirmed?
The output object is never changed, so it always matches your schema. The flag is in the run's summary, which the session_completed event carries in GET /v1/sessions/{id}/events. A summary that contains Couldn't confirm on screen names the values to treat as unverified.