how can i handle errors from raw_schema validation...
# ask-ai
r
how can i handle errors from raw_schema validations such as below raised from a function tool
Copy code
livekit.agents._exceptions.APIStatusError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'tools.2.custom.input_schema.type: Field required'}, 'request_id': 'req_011CSZwaBHxxBWN94yeDMv68'} (status_code=400, request_id=req_011CSZwaBHxxBWN94yeDMv68, body={'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'tools.2.custom.input_schema.type: Field required'}, 'request_id': 'req_011CSZwaBHxxBWN94yeDMv68'}, retryable=False)
t
Thanks for reaching out, John. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To handle errors from raw_schema validations in LiveKit, especially those APIStatusError with error code 400 and type invalid_request_error, here’s a friendly step-by-step guide to help you out: 1. Understand the Context: •
raw_schema
is the JSON schema you define when registering raw function tools using
@llm.raw_function_tool
. Your inputs need to match this schema to work smoothly. 2. Handle Validation Errors: • When you catch an
APIStatusError
, take a close look at the response body for details about what went wrong. Here’s a quick example:
Copy code
from livekit.agents import APIStatusError

try:
    result = await agent.execute(input_data)
except APIStatusError as e:
    if e.status_code == 400 and e.body:
        error_data = e.body
        if error_data.get('type') == 'invalid_request_error':
            print(f"Schema validation failed: {error_data.get('message')}")
3. Validate Inputs Before Sending: • It’s a great idea to check your inputs against the schema before making API calls. You can use libraries like
jsonschema
for this. Here’s how:
Copy code
from jsonschema import validate, ValidationError

schema = {
    "type": "object",
    "properties": {"location": {"type": "string"}},
    "required": ["location"]
}

try:
    validate(input_data, schema)
except ValidationError as ve:
    print("Invalid input:", ve)
4. Best Practices: • Keep an eye on error logs to pinpoint exactly which validation rule caused trouble. • Make sure your
raw_schema
has clear field names and validation rules to avoid confusion. Since LiveKit doesn’t have built-in tools for pre-validating inputs against raw_schema, adding your own validation and error handling steps can really save you headaches. If you want, I’m here to help you set up a pre-validation layer—just let me know! Sources: tools/server/server.cpp | livekit-plugins/livekit-plugins-tavus/livekit/plugins/tavus/api.py