Skip to content

Form field detection ​

A workflow that takes an image or PDF of any form and returns the fields to fill, each with the box where its value goes, plus an annotated preview. It is built from three reusable agents and one workflow. Every step returns the same JSON shape, so steps can be swapped (OCR-based or vision-based extraction) and reused in other workflows.

text
start (form file)
  → ocr                       lines and words with boxes
  → fields  (agent)           form-fields-ocr: fields with boxes
  → review  (loop)            form-fields-reviewer until approved (max 3)
  → confirm (approval)        optional: a person corrects the fields
  → preview (annotate)        boxes drawn on the form
  → output                    { fields, previewFileId }

The shared output shape ​

Every extraction and review step answers with this schema (use it as the agents' outputSchema):

json
{
  "type": "object",
  "properties": {
    "fields": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "key": { "type": "string", "description": "snake_case id, e.g. date_of_birth" },
          "label": { "type": "string", "description": "the label as printed on the form" },
          "type": {
            "type": "string",
            "enum": ["text", "date", "number", "checkbox", "signature", "choice", "other"]
          },
          "bbox": {
            "type": "object",
            "description": "where the VALUE is written; fractions (0-1) of the page, x,y = top-left",
            "properties": {
              "page": { "type": "integer" },
              "x": { "type": "number" },
              "y": { "type": "number" },
              "w": { "type": "number" },
              "h": { "type": "number" }
            },
            "required": ["page", "x", "y", "w", "h"]
          },
          "labelLineId": { "type": "string", "description": "OCR line id of the label, if any" },
          "confidence": { "type": "number", "description": "0-1" }
        },
        "required": ["key", "label", "type", "bbox", "confidence"]
      }
    },
    "approved": { "type": "boolean", "description": "reviewer only: true when nothing changed" },
    "notes": { "type": "string" }
  },
  "required": ["fields"]
}

Agent 1: form-fields-ocr ​

Type extraction, a chat model, the schema above. Input schema: { "type": "object", "properties": { "lines": { "type": "array" } }, "required": ["lines"] }.

Instructions:

text
You find the fields a person has to fill in on a form.

You get the form's OCR lines as JSON: each line has an id, its text and a bbox
{x, y, w, h} as fractions (0-1) of the page, x/y being the top-left corner, and the page number.

For every field to fill in (text boxes, blank lines, date fields, checkboxes, signature areas):
- key: a snake_case id; label: the printed label; type: the kind of value.
- bbox: where the VALUE is written, not the label. It is usually right of the label on the same
  line (starting a little after the label's right edge, up to the next label or the page margin),
  or just below it. Use the label's height for the box height. A checkbox is a small square left
  of its label.
- labelLineId: the id of the label's line.
- confidence: how sure you are about the box (0-1).
Ignore titles, instructions and text that is not a field label. Do not invent fields.

OCR lines:
{{lines}}

Agent 2: form-fields-vision ​

The same, without OCR: type extraction, a model with the vision capability, the same output schema, files with strategy: "vision". Instructions as above without the OCR part. Boxes from images alone are less precise than boxes grounded on OCR words; use it for forms OCR cannot read (handwriting, photos), or send both the image and the OCR lines to one agent.

Agent 3: form-fields-reviewer ​

Type extraction, a vision model, the same output schema, files with strategy: "vision". Input schema: { "type": "object", "properties": { "draft": { "type": "object" } } }.

Instructions:

text
You check fields detected on a form. You get the form image and a draft of its fields with boxes
(fractions 0-1 of the page, x/y top-left). The second image, if any, shows the draft's boxes
drawn on the form.

Fix boxes that do not cover the place where the value is written, add fields that are missing,
remove things that are not fields. Return all fields. Set approved to true only when the draft
needed no change.

Draft:
{{draft}}

The workflow ​

Create a workflow agent (accepting PNG, JPEG and PDF files) with this graph; replace the agent ids:

json
{
  "nodes": [
    { "id": "start", "type": "start" },
    { "id": "ocr", "type": "ocr", "file": "$files[0]", "maxPages": 3 },
    {
      "id": "fields",
      "type": "agent",
      "agentId": "<form-fields-ocr id>",
      "input": {
        "lines": "[$nodes.ocr.output.pages.( $p := page; lines.{ \"id\": id, \"page\": $p, \"text\": text, \"bbox\": bbox } )]"
      }
    },
    {
      "id": "review",
      "type": "loop",
      "agentId": "<form-fields-reviewer id>",
      "input": { "draft": "$last ? $last : $nodes.fields.output" },
      "files": "$files[0]",
      "until": "$last.approved = true",
      "maxIterations": 3
    },
    { "id": "preview", "type": "annotate", "file": "$files[0]", "boxes": "$nodes.review.output" },
    {
      "id": "out",
      "type": "output",
      "output": "{ \"fields\": $nodes.review.output.fields, \"previewFileId\": $nodes.preview.output.fileId }"
    }
  ],
  "edges": [
    { "id": "e1", "source": "start", "target": "ocr" },
    { "id": "e2", "source": "ocr", "target": "fields" },
    { "id": "e3", "source": "fields", "target": "review" },
    { "id": "e4", "source": "review", "target": "preview" },
    { "id": "e5", "source": "preview", "target": "out" }
  ]
}

Run it with a file:

bash
FILE=$(curl -s -H "Authorization: Bearer $KEY" -F file=@form.png https://api.example.com/v1/files | jq -r .id)
curl -s -H "Authorization: Bearer $KEY" -H 'content-type: application/json' \
  -d "{\"files\": [\"$FILE\"]}" https://api.example.com/v1/agents/form-fields/runs

The output holds the fields and previewFileId; GET /v1/files/{previewFileId} gives a download link of the annotated form. In the console, the playground and the run view show the preview.

Variations ​

  • Let the reviewer see the boxes. A loop runs a single agent, so a preview cannot be drawn inside it. Add an annotate node (draft_preview) before the loop and pass its images with the form: "files": "[$files[0], $nodes.draft_preview.output]".
  • Human confirmation. Add an approval node with data: $nodes.review.output between the loop and the annotate node, and annotate $nodes.confirm.output: the approver's edits are used.
  • Vision only. Replace ocr and fields with an agent node running form-fields-vision with files: "$files[0]".
  • Reuse. Publish the workflow and run it from other workflows with an agent node and files.

Hevkar public API v1