How do I add validation steps for each agent in a ...
# ask-ai
m
How do I add validation steps for each agent in a declarative flow? and make sure it gets all the info it needs for each step before moving on to the next step - i.e. userdata fields for a step is filled
t
Thanks for reaching out, Nikki. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking to add validation steps for each agent in your declarative flow! To make sure userdata fields are properly filled before moving on, you can include validation logic right within the function tools of each agent. This way, you ensure that required fields meet the criteria you set (like being non-empty or correctly formatted) before the flow transitions. The validation happens inside the
@function_tool
method, where you check the input and can return
None
to keep the user in the current agent until the data is valid. For example, in a
CollectNameAgent
, the
collect
tool might look like this to make sure the name isn’t empty:
Copy code
@function_tool
async def collect(self, value: str) -> Optional[Agent]:
    value = value.strip()
    if not value:
        await self.session.say("I didn't catch that. Please tell me your name.")
        return None  # Stay in current agent

    sd: SurveyData = self.session.userdata
    sd.record(self.label, value)
    self.session.state[self.key] = value
    return await self.transition()
Similarly, for validating emails in
CollectEmailAgent
, you can do a simple format check like this:
Copy code
@function_tool
async def collect(self, value: str) -> Optional[Agent]:
    value = value.strip().lower()
    if "@" not in value or "." not in value:
        await self.session.say("That doesn't seem like a valid email. Please try again.")
        return None

    sd: SurveyData = self.session.userdata
    sd.record(self.label, value)
    self.session.state[self.key] = value
    return await self.transition()
To help make your validation even more effective, here are a few tips: • Use
@function_tool
methods to both validate and clean up input. • Return
None
when the input isn’t valid yet, so users stay where they need to be. • Leverage
session.userdata
with structured
dataclass
models to keep track of progress and completeness. • Apply formatting helpers like
proper_capitalize
to keep data consistent and neat. If you’d like, I’d be happy to help you set up validation rules for other data types, like phone numbers or dates. Or, if you want to enhance the user experience with frontend feedback—maybe using RPC to highlight form fields—I can guide you through that too. Just let me know what you need! Sources: flows/README.md | Workflows | LiveKit Docs | Tool definition and use | LiveKit Docs
m
yes phone numbers, birthdates, also address validation, and UUID formatting return as a alphanumeric instead of words
t
Hi there! To help you confidently validate phone numbers, birthdates, addresses, and UUID formatting in a declarative flow, here are some friendly approaches you can try: 1. UUID Validation You can use JSON Schema with
format: "uuid"
to make sure the input matches the standard UUID format (like
12345678-1234-1234-1234-1234567890ab
):
Copy code
{
  "type": "string",
  "format": "uuid"
}
2. Birthdate Validation For birthdates, using
format: "date"
works great for the
YYYY-MM-DD
format, or
format: "date-time"
if you need full date-time inputs:
Copy code
{
  "type": "string",
  "format": "date"
}
3. Phone Number Validation • Try using the
allowed_numbers
field in your SIP trunk configuration to restrict calls to numbers you’ve already validated. • Also, during agent workflows, confirming phone numbers explicitly during interactions before saving them really helps keep things accurate. 4. Address Validation Address validation isn’t built-in natively, but here are some friendly ways to handle it: • Custom Regex Patterns: You can create structured tool parameters with clear hints like "street, city, state, ZIP code" to guide users. • External Validation: Using geocoding APIs like Google Maps or postal code libraries can help verify addresses after you capture them. • Two-Step Confirmation: Asking users to confirm their address after you read it back is a simple and effective way to catch errors. 5. Declarative Flow Integration Another great tip is defining structured input tools with
zod
in TypeScript or
pydantic
in Python. These help enforce your validation rules, and LiveKit can convert these into grammar rules that guide input during conversations. If you'd like, I’d be happy to walk you through detailed examples tailored to your specific workflow. Just let me know! Sources: examples/src/restaurant_agent.ts | examples/voice_agents/restaurant_agent.py | SIP Integration & Telephony | livekit/livekit_composite | DeepWiki | tests/test-grammar-llguidance.cpp | SIP APIs | LiveKit Docs