I’m having trouble with Azure AI Agents when I try to use custom functions that are defined in separate Python modules. I created some custom functions and used FunctionTool to wrap them, then put them in a ToolSet. Here’s my setup:
{
method1,
method2,
method3,
method4,
method5
})
custom_tool_collection = ToolSet()
custom_tool_collection.add(custom_functions_tool)
ai_agent = project_client.agents.create_agent(
model=os.environ.get("AZURE_AOAI_CHAT_MODEL_NAME_DEPLOYMENT_ID"),
name="custom-function-agent",
instructions="""
You are a helpful assistant that can execute custom functions when needed.
""",
toolset=custom_tool_collection,
)
The problem is I keep getting this error when I run the agent:
ValueError: Toolset is not available in the client.
What’s weird is that if I put everything in one Python file instead of splitting it between tools.py and main.py, it works perfectly fine. This makes me think the issue happens when I import the ToolSet from another module.
What I want to know:
- Why does this error happen when I use a
ToolSet from a different file?
- How can I fix this so I can keep my tools in a separate module?
Things I already tried:
- Made sure the import statement works correctly
- Double checked all my environment variables
- Verified the
ToolSet is built properly before importing
I ran into something similar a few months back and it turned out to be a serialization issue. The Azure AI client needs to serialize the ToolSet when sending it to the service, but when you import from another module, the function references can get mangled during the process. What worked for me was recreating the ToolSet in the same file where I initialize the agent, even if the actual function definitions live elsewhere. So instead of importing a pre-built ToolSet, import your individual functions and then construct the ToolSet locally: from tools import method1, method2, method3, method4, method5 custom_functions_tool = FunctionTool({ method1, method2, method3, method4, method5 }) custom_tool_collection = ToolSet(); custom_tool_collection.add(custom_functions_tool). This way the ToolSet object gets created in the same execution context as the agent initialization, which seems to resolve the serialization problems. The Azure client can properly introspect the function metadata when everything is in the same module scope.
This error typically stems from how the Azure AI client handles function introspection when the ToolSet crosses module boundaries. The client performs runtime validation on the functions within your ToolSet, but importing a pre-constructed ToolSet can break the function metadata chain that Azure needs. I encountered this same issue when restructuring my project and found that the problem occurs because Python’s module loading mechanism affects how Azure’s internal reflection works on imported objects. The client expects to be able to inspect function signatures and docstrings directly, but cross-module imports can obscure this metadata. Try defining your ToolSet construction logic as a factory function in your tools module, then call that function from your main file rather than importing a pre-built object. This ensures the ToolSet gets created within the same execution context where you initialize the agent, preserving the metadata chain Azure requires for validation. I’ve found this approach works consistently across different project structures.
Been there, dealt with this exact headache last year. The issue is actually about how Python handles object references across module boundaries when Azure tries to validate the toolset.
What’s happening is the Azure client does some internal validation on the ToolSet object, and it expects certain metadata to be accessible. When you import a pre-built ToolSet from another file, Python’s import mechanism can break these internal references.
Here’s what fixed it for me - use lazy initialization. Instead of building the ToolSet in your tools.py file, create a function that returns it:
# tools.py
def get_custom_toolset():
custom_functions_tool = FunctionTool({
method1,
method2,
method3,
method4,
method5
})
tool_collection = ToolSet()
tool_collection.add(custom_functions_tool)
return tool_collection
# main.py
from tools import get_custom_toolset
custom_tool_collection = get_custom_toolset()
This way the ToolSet gets instantiated in your main execution context, not during import time. The Azure client can then properly inspect all the function signatures and metadata it needs.
I’ve used this pattern in production for about 8 months now across multiple projects and never had the error come back.