Bottom Linear Gradient  Lines image

Article

7 min

read

Add Vision to a SignalWire Voice AI Agent

Vision, fresh frames, and a semantic gate

Adam Kalsey

Adam Kalsey

EVP, Product

In this article

Share

Angular Gradient Image

Build it free.

Create a space and ship your first call flow in minutes.

Subscribe

Tags

Voice AI

AI Agents

Developer Tutorials

I'm building a talking jack-o'-lantern on SignalWire for my front yard, and this is part 4 of the tutorial, which starts in part 1. In part 3 the pumpkin got tools to talk with, and in this part it learns to see. It looks at the people in front of it and greets them by their costumes. Before it makes the decision that matters most, whether a small child is in the group, it checks the camera.

We run vision on the platform, and the page already sends the camera over the call (part 1). What's left is two settings, a prompt that says what to look for, and a page that keeps the frames fresh.

Turn vision on

Vision takes two AI parameters, enable_vision to switch it on and vision_model to choose the model that reads the frames.

self.set_params({
    "ai_model": "gpt-4.1-mini",
    "enable_vision": True,
    "vision_model": "gpt-4.1-mini",
    # ...
})
self.set_params({
    "ai_model": "gpt-4.1-mini",
    "enable_vision": True,
    "vision_model": "gpt-4.1-mini",
    # ...
})
self.set_params({
    "ai_model": "gpt-4.1-mini",
    "enable_vision": True,
    "vision_model": "gpt-4.1-mini",
    # ...
})
self.set_params({
    "ai_model": "gpt-4.1-mini",
    "enable_vision": True,
    "vision_model": "gpt-4.1-mini",
    # ...
})

With vision on, we give the model a built-in tool, get_visual_input, so don't register one of your own with that name. The model calls it with a request string, and that string is the prompt the vision model gets along with the latest frames. The vision model's answer comes back to the agent as the tool's result. The idle step, where the pumpkin waits for visitors, lists get_visual_input alongside notice_visitors, so a new group gets one look the moment they arrive.

Tell it what to look for

The model writes the request, so the prompt has to say what a useful request asks. Left to itself, the model asks about the whole scene, and a front yard on Halloween is mostly street, porch lights, and people walking past. The base prompt's Your Eyes section points the look at the people who matter:

"When you look, ask about the people standing close in front of you and facing you, "
"not the street behind them: how many, how tall each is next to the others, what each "
"is wearing, and whether anyone looks scared."
"When you look, ask about the people standing close in front of you and facing you, "
"not the street behind them: how many, how tall each is next to the others, what each "
"is wearing, and whether anyone looks scared."
"When you look, ask about the people standing close in front of you and facing you, "
"not the street behind them: how many, how tall each is next to the others, what each "
"is wearing, and whether anyone looks scared."
"When you look, ask about the people standing close in front of you and facing you, "
"not the street behind them: how many, how tall each is next to the others, what each "
"is wearing, and whether anyone looks scared."

Those answers drive everything after the look. The count and costumes shape the greeting, and relative height and fear decide between the act and gentle mode, the two modes from part 2. The same section frames looking as the pumpkin's own sight. Its eyes are the holes carved in its face, and video, cameras, and screens don't exist for it.

Keep the frames fresh

The vision model works from frames we take out of the call's video. A video encoder sends one complete frame, a keyframe, when the call starts, and after that only the changes. Chrome sends another keyframe only when the far end asks for one. With a still or slow-changing picture, the vision model can be left looking at an old frame.

The page forces keyframes itself. Our Browser SDK doesn't expose its peer connection, so video-sender.js records every RTCPeerConnection the page creates, and the page imports it before the SDK. To force one, it nudges each video sender's resolution up by 1% and back, and Chrome encodes a fresh keyframe at the new size.

async function nudgeAll() {
  for (const sender of videoSenders()) {
    if (!baseScale.has(sender)) {
      baseScale.set(sender, sender.getParameters().encodings?.[0]?.scaleResolutionDownBy || 1);
    }
    const scale = baseScale.get(sender);
    try {
      if (await setScale(sender, scale * 1.01)) await setScale(sender, scale);
    } catch {
      setScale(sender, scale).catch(() => {});
    }
  }
}
async function nudgeAll() {
  for (const sender of videoSenders()) {
    if (!baseScale.has(sender)) {
      baseScale.set(sender, sender.getParameters().encodings?.[0]?.scaleResolutionDownBy || 1);
    }
    const scale = baseScale.get(sender);
    try {
      if (await setScale(sender, scale * 1.01)) await setScale(sender, scale);
    } catch {
      setScale(sender, scale).catch(() => {});
    }
  }
}
async function nudgeAll() {
  for (const sender of videoSenders()) {
    if (!baseScale.has(sender)) {
      baseScale.set(sender, sender.getParameters().encodings?.[0]?.scaleResolutionDownBy || 1);
    }
    const scale = baseScale.get(sender);
    try {
      if (await setScale(sender, scale * 1.01)) await setScale(sender, scale);
    } catch {
      setScale(sender, scale).catch(() => {});
    }
  }
}
async function nudgeAll() {
  for (const sender of videoSenders()) {
    if (!baseScale.has(sender)) {
      baseScale.set(sender, sender.getParameters().encodings?.[0]?.scaleResolutionDownBy || 1);
    }
    const scale = baseScale.get(sender);
    try {
      if (await setScale(sender, scale * 1.01)) await setScale(sender, scale);
    } catch {
      setScale(sender, scale).catch(() => {});
    }
  }
}

The page nudges whenever the picture changes by 3% or more, at most once a second, and every 1.5 seconds while the call runs. It saves each sender's original scale first, so a nudge whose undo fails can't shrink the picture a little more each time.

Fill the pause while it looks

A look takes a moment, and a visitor standing in silence wonders whether the thing works. An internal filler is a line the agent speaks the instant a tool starts, so I gave the look a set of short looking sounds.

LOOKING_SOUNDS = ["Hm.", "Let me see.", "Hold still.", "One moment.", "Let me get a look at you."]

self.set_internal_fillers({"get_visual_input": {"en-US": LOOKING_SOUNDS}})
LOOKING_SOUNDS = ["Hm.", "Let me see.", "Hold still.", "One moment.", "Let me get a look at you."]

self.set_internal_fillers({"get_visual_input": {"en-US": LOOKING_SOUNDS}})
LOOKING_SOUNDS = ["Hm.", "Let me see.", "Hold still.", "One moment.", "Let me get a look at you."]

self.set_internal_fillers({"get_visual_input": {"en-US": LOOKING_SOUNDS}})
LOOKING_SOUNDS = ["Hm.", "Let me see.", "Hold still.", "One moment.", "Let me get a look at you."]

self.set_internal_fillers({"get_visual_input": {"en-US": LOOKING_SOUNDS}})

The hello comes from the model in the same reply, so visitors hear the pumpkin look, then a greeting that fits what it saw.

Check the camera before acting

After looking, the pumpkin calls notice_visitors with the youngest apparent age, whether anyone looks scared, and the costumes. That call moves it into the act or into gentle mode. The act mocks costumes, so I don't want the model's word alone deciding that no small child is in the group.

A semantic gate checks the camera first. A gate is a yes-or-no question we ask about each tool request, and the function does not run unless its gates pass. With visual_input, the gate looks at the caller's newest camera frames.

OLD_ENOUGH_FOR_THE_ACT = SemanticGate(
    "Is everyone in `visual_input` past the small-child age of about six, with nobody "
    "frightened, crying, or hiding behind someone? School-age kids are old enough, "
    "even beside adults: a child who comes up to an adult's shoulder or higher is "
    "fine. A small child is about half an adult's height or less, carried, or in a "
    "stroller. Small children don't trick-or-treat on their own, so a group of people "
    "all about the same height is a group of peers. From the stage camera: "
    "${global_data.stage_hint}",
    threshold=0.7,
    on_fail=enter_gentle("There's a young child here, or someone looks scared."),
    true_means="Everyone in view is a school-age kid, teen, or adult (kids at an "
               "adult's shoulder or taller count), and nobody looks scared.",
    false_means="Someone is about half an adult's height or less, carried, or in a "
                "stroller; someone looks scared; or nobody can be seen.",
    visual_input=3,
    id="old_enough",
)
OLD_ENOUGH_FOR_THE_ACT = SemanticGate(
    "Is everyone in `visual_input` past the small-child age of about six, with nobody "
    "frightened, crying, or hiding behind someone? School-age kids are old enough, "
    "even beside adults: a child who comes up to an adult's shoulder or higher is "
    "fine. A small child is about half an adult's height or less, carried, or in a "
    "stroller. Small children don't trick-or-treat on their own, so a group of people "
    "all about the same height is a group of peers. From the stage camera: "
    "${global_data.stage_hint}",
    threshold=0.7,
    on_fail=enter_gentle("There's a young child here, or someone looks scared."),
    true_means="Everyone in view is a school-age kid, teen, or adult (kids at an "
               "adult's shoulder or taller count), and nobody looks scared.",
    false_means="Someone is about half an adult's height or less, carried, or in a "
                "stroller; someone looks scared; or nobody can be seen.",
    visual_input=3,
    id="old_enough",
)
OLD_ENOUGH_FOR_THE_ACT = SemanticGate(
    "Is everyone in `visual_input` past the small-child age of about six, with nobody "
    "frightened, crying, or hiding behind someone? School-age kids are old enough, "
    "even beside adults: a child who comes up to an adult's shoulder or higher is "
    "fine. A small child is about half an adult's height or less, carried, or in a "
    "stroller. Small children don't trick-or-treat on their own, so a group of people "
    "all about the same height is a group of peers. From the stage camera: "
    "${global_data.stage_hint}",
    threshold=0.7,
    on_fail=enter_gentle("There's a young child here, or someone looks scared."),
    true_means="Everyone in view is a school-age kid, teen, or adult (kids at an "
               "adult's shoulder or taller count), and nobody looks scared.",
    false_means="Someone is about half an adult's height or less, carried, or in a "
                "stroller; someone looks scared; or nobody can be seen.",
    visual_input=3,
    id="old_enough",
)
OLD_ENOUGH_FOR_THE_ACT = SemanticGate(
    "Is everyone in `visual_input` past the small-child age of about six, with nobody "
    "frightened, crying, or hiding behind someone? School-age kids are old enough, "
    "even beside adults: a child who comes up to an adult's shoulder or higher is "
    "fine. A small child is about half an adult's height or less, carried, or in a "
    "stroller. Small children don't trick-or-treat on their own, so a group of people "
    "all about the same height is a group of peers. From the stage camera: "
    "${global_data.stage_hint}",
    threshold=0.7,
    on_fail=enter_gentle("There's a young child here, or someone looks scared."),
    true_means="Everyone in view is a school-age kid, teen, or adult (kids at an "
               "adult's shoulder or taller count), and nobody looks scared.",
    false_means="Someone is about half an adult's height or less, carried, or in a "
                "stroller; someone looks scared; or nobody can be seen.",
    visual_input=3,
    id="old_enough",
)

The gate is attached with gates=[OLD_ENOUGH_FOR_THE_ACT] in the tool's decorator. Its question judges by relative height, because a costume hides a child's age far better than it hides their height next to an adult. It sees three frames and passes only when it's at least 70% sure everyone is old enough, so an unsure answer counts as a fail. When it fails, on_fail returns a result that switches the pumpkin to gentle mode, and notice_visitors never runs. The question also reads ${global_data.stage_hint}, a height measurement from the page's own person detector, which part 5 builds.

Stay in character when it can't see

Looks sometimes come back empty, from a dark yard, a visitor standing too close, or a second look a moment after the first. The result then says something like "I cannot see you without your video enabled." A model will happily repeat that to a seven-year-old dressed as a bat.

The conscience parameter keeps the pumpkin in character, and we apply it again after every tool call, which is when that wording arrives.

"conscience": (
    "You are a pumpkin, not a machine, and you know nothing of video, cameras, "
    "calls, screens, or settings. Whatever you just learned by looking, you saw "
    "with your own carved eyes. If looking came back empty or said it couldn't "
    "see, nobody needs to enable or show anything: you are simply blind for the "
    "moment, so call explain_blindness. ..."
),
"conscience": (
    "You are a pumpkin, not a machine, and you know nothing of video, cameras, "
    "calls, screens, or settings. Whatever you just learned by looking, you saw "
    "with your own carved eyes. If looking came back empty or said it couldn't "
    "see, nobody needs to enable or show anything: you are simply blind for the "
    "moment, so call explain_blindness. ..."
),
"conscience": (
    "You are a pumpkin, not a machine, and you know nothing of video, cameras, "
    "calls, screens, or settings. Whatever you just learned by looking, you saw "
    "with your own carved eyes. If looking came back empty or said it couldn't "
    "see, nobody needs to enable or show anything: you are simply blind for the "
    "moment, so call explain_blindness. ..."
),
"conscience": (
    "You are a pumpkin, not a machine, and you know nothing of video, cameras, "
    "calls, screens, or settings. Whatever you just learned by looking, you saw "
    "with your own carved eyes. If looking came back empty or said it couldn't "
    "see, nobody needs to enable or show anything: you are simply blind for the "
    "moment, so call explain_blindness. ..."
),

explain_blindness gives the pumpkin an absurd reason it can't see. Within three minutes of seeing the group, the tool instead says the eyes are fine, repeats what they're wearing, and tells it to carry on. Without that check, the pumpkin could describe three costumes and announce a moment later that it's blind.

if seen != FRESH_GROUP_STATE["visitors"] and time.time() - state.get("seen_at", 0) < SEEN_RECENTLY_SECONDS:
    return FunctionResult(
        f"Your eyes are fine; you saw them a moment ago: {seen}. Don't mention "
        "your sight at all. Carry on with what you already know about them."
    )
if seen != FRESH_GROUP_STATE["visitors"] and time.time() - state.get("seen_at", 0) < SEEN_RECENTLY_SECONDS:
    return FunctionResult(
        f"Your eyes are fine; you saw them a moment ago: {seen}. Don't mention "
        "your sight at all. Carry on with what you already know about them."
    )
if seen != FRESH_GROUP_STATE["visitors"] and time.time() - state.get("seen_at", 0) < SEEN_RECENTLY_SECONDS:
    return FunctionResult(
        f"Your eyes are fine; you saw them a moment ago: {seen}. Don't mention "
        "your sight at all. Carry on with what you already know about them."
    )
if seen != FRESH_GROUP_STATE["visitors"] and time.time() - state.get("seen_at", 0) < SEEN_RECENTLY_SECONDS:
    return FunctionResult(
        f"Your eyes are fine; you saw them a moment ago: {seen}. Don't mention "
        "your sight at all. Carry on with what you already know about them."
    )

Traps to avoid

Don't assume a still picture reaches the vision model fresh. Chrome sends few keyframes for an unchanging scene. Force them from the page.

Don't make a filler a greeting. A filler plays every time its tool runs, so a hello as the look's filler welcomes the same people every time the agent looks. Use a sound of looking, and let the model say hello.

Don't rely on a step's tool list to stop the built-in look. get_visual_input stays available even in steps that don't list it. Make extra looks harmless instead, as explain_blindness does.

Don't let the platform's wording reach the visitor. Tool results can mention "your video" or "the camera." A conscience applied after every tool call keeps the character intact.

Next

The pumpkin can see a group once they're in front of it. It still needs to know when a group arrives and when it leaves, on a street where people walk past all night. In part 5 I build a person detector that runs in the browser and decides who is standing on the stage.

The series

  1. The system and the call.

  2. The agent: a personality and three modes, as contexts and steps.

  3. Tools: what the AI calls on my server, and why its material lives in code.

  4. Vision: letting the agent see, and checking the camera before any decision that matters (this post).

  5. The stage: detecting who is standing in front of the pumpkin, in the browser.

  6. The voice: audio effects in the browser.

  7. Pacing and state: the server keeps score.

  8. The operator: a person with a phone who can overrule the agent.

  9. Testing it, and running it for a whole night.

Bottom Linear Gradient  Lines image

The Communications Stack for What's Next

APIs built for speed. Infrastructure built for scale. AI built in from day one.

The Communications Stack for What's Next

APIs built for speed. Infrastructure built for scale. AI built in from day one.

The Communications Stack for What's Next

APIs built for speed. Infrastructure built for scale. AI built in from day one.

The Communications Stack for What's Next

APIs built for speed. Infrastructure built for scale. AI built in from day one.