Skip to main content
Sometimes you need more than a simple string back from the agent — you need a reliable, typed object you can pass directly to your API or database. That’s where agent.extract() shines: it reads the current screen and maps what it sees onto a JSON schema you provide, giving you structured, schema-validated data instead of free-form text. Combining agent.extract() with a Pydantic Result model means the runtime validates the returned values for you before the workflow finishes. This example follows a four-phase pattern:
1

Navigate

agent.execute() searches for the target patient and opens their profile.
2

Verify

agent.verify() confirms the correct profile is on screen before attempting any extraction — preventing silent mis-reads on the wrong page.
3

Extract demographics & visits

Two agent.extract() calls, each with a tailored JSON schema, pull the demographic fields and the visit history as typed objects rather than raw strings.
4

Return

The extracted values are assembled into a validated Result and returned to the caller as structured JSON.
workflow.py
from nen import Agent
from pydantic import BaseModel, Field


class Params(BaseModel):
    patient_name: str = Field(min_length=1)


class Result(BaseModel):
    demographics: dict | None = None
    visits: list[dict] = []


def run(params: Params) -> Result:
    agent = Agent()

    # Navigate to patient profile
    agent.execute(f"Search for and open patient '{params.patient_name}'")

    if not agent.verify(f"Is patient profile for '{params.patient_name}' visible?"):
        raise RuntimeError("Patient not found")

    # Extract patient demographics
    demographics = agent.extract(
        "Extract the patient's demographic information",
        schema={
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "date_of_birth": {"type": "string"},
                "phone": {"type": "string"},
                "email": {"type": "string"},
                "address": {"type": "string"}
            }
        }
    )

    # Extract list of recent visits
    visits = agent.extract(
        "Extract the list of recent visits/appointments",
        schema={
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "date": {"type": "string"},
                    "reason": {"type": "string"},
                    "provider": {"type": "string"}
                }
            }
        }
    )

    return Result(demographics=demographics, visits=visits)