When function calling in python using @function_to...
# ask-ai
l
When function calling in python using @function_tool, it seems like the AI doesn’t always match the schema. OpenAI has strict mode, meaning that the AI HAS to match the schema. OpenAI recommends that it’s turned on. How does Livekit handle this? Can I enforce strict mode? What does @function_tool do under the hood?
t
Thanks for reaching out, Tom. 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 interested in the
@function_tool
decorator in LiveKit! It’s a handy feature that lets Python-based AI agents share their methods as callable tools for language models (LLMs). Behind the scenes, it uses Python's
inspect
module to understand function signatures and type hints, then creates a Pydantic model that’s converted into a JSON Schema so LLMs can work with it smoothly. By default, it uses a legacy schema, but you also have the option to enable strict mode for a more precise schema adherence. Strict mode is really helpful because it makes sure the LLM follows the defined parameters exactly. It does this by setting
additionalProperties
to
false
, marking all required fields clearly, and managing optional fields with
null
types. This way, it boosts reliability by preventing the LLM from making up or skipping parameters and helps catch type mismatches early on. If you’d like to try strict mode, you can use the
build_strict_openai_schema
function, which tweaks the JSON schema to meet OpenAI’s strict mode requirements. Feel free to ask if you want more details or some examples—I’m here to help! Sources: docs/index.yaml | livekit-agents/livekit/agents/voice/agent.py | Agents v0.x migration guide - Python | LiveKit Docs
l
How do I enable it in my python appo
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!