When using raw_scheme to build tools, how can I pa...
# ask-ai
h
When using raw_scheme to build tools, how can I pass additional arguments to the function which is passed to function_tool()
t
Thanks for reaching out, Shawn. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want to pass extra arguments to a function using
function_tool()
with
raw_schema
, the key is to define all your parameters inside the
parameters
field of the
raw_schema
dictionary. Then, you can grab these parameters from the
raw_arguments
dictionary in your function handler. Here’s a friendly step-by-step: • Start by defining each parameter in the
parameters
>
properties
section of the
raw_schema
. For example, you might include things like
city
and
units
. • When you’re inside your function handler, access these parameters using
raw_arguments["parameter_name"]
. If a parameter is optional, you can use
raw_arguments.get("parameter_name", "default_value")
to set a fallback. • Don’t forget to list any required parameters in the
required
array within the schema. So, when the tool runs, it reads the JSON object that matches your schema and hands it over as the
raw_arguments
dictionary to your function. This way, you can easily manage multiple arguments without any fuss. Sources: Tool definition and use | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py
h
I want to pass addtional arguments which are not part of the raw_arguments
t
If you want to pass extra arguments to a function that aren't part of
raw_arguments
, you can use the
RunContext
parameter in your function. This handy feature lets you tap into external info like session details or custom user data. Here’s a simple way to do it: • Add a
context: RunContext
parameter to your function signature. The framework automatically takes care of this, so the LLM won’t see it. • Use the
context.userdata
property to save and grab custom data. For instance, you can set up
userdata
with your extra arguments when you start the agent session. • Inside your function, access
userdata
or other session info to work with those additional arguments. If you’d like me to walk you through an example or explain anything further, just give me a shout! Sources: Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs