How can I create a tool in the new foundry python code for openai

AQ 20 Reputation points
2025-12-05T12:50:34.9933333+00:00

Hello, I’m following this tutorial: https://learn.microsoft.com/en-us/azure/ai-foundry/agents/how-to/tools/knowledge-retrieval?view=foundry&tabs=foundry%2Cpython#create-an-agent-with-the-mcp-tool

I was able to do RAG works properly, but I was not able to create a tool for this version of API. How can I do it? Basically I would like to do a function calling…Thank you

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-12-05T12:51:51.07+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To create a tool in the Azure AI Foundry using Python, you can define a function that you want the agent to call when needed. This is done by passing the function to the agent's tools parameter during the agent's creation. Here’s a simple example of how to create a function tool:

    1. Define Your Function: Create a function that performs the desired task. You can use type annotations to provide descriptions for the parameters.
         from typing import Annotated
         from pydantic import Field
         
         def get_weather(
             location: Annotated[str, Field(description="The location to get the weather for."))
         ) -> str:
             return f"The weather in {location} is cloudy with a high of 15°C."
      
    2. Use the ai_function Decorator: Optionally, you can use the ai_function decorator to specify the function's name and description explicitly.
         from agent_framework import ai_function
         
         @ai_function(name="weather_tool", description="Retrieves weather information for any location")
         def get_weather(...):
             ...  # function implementation
      
    3. Create the Agent: When creating the agent, pass the function tool to the tools parameter.
         import asyncio
         from agent_framework.azure import AzureOpenAIChatClient
         from azure.identity import AzureCliCredential
         
         agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
             instructions="You are a helpful assistant",
             tools=get_weather
         )
      
    4. Run the Agent: You can now run the agent, and it will be able to call the get_weather function when needed.
         async def main():
             result = await agent.run("What is the weather like in Amsterdam?")
             print(result.text)
         
         asyncio.run(main())
      

    This approach allows you to create custom function tools that your agent can utilize effectively.

    0 comments No comments

  2. Anshika Varshney 3,795 Reputation points Microsoft External Staff Moderator
    2025-12-05T13:44:36.5266667+00:00

    Hey AQ,

    It looks like you're trying to create a tool in the Foundry Python code for OpenAI and are specifically interested in function calling. Here's a detailed guide to help you get started:

    Creating a Tool in Foundry

    1. Set Up Your Environment:
      • Ensure you're using the latest version of the azure-ai-projects library. You may want to create a virtual environment and install the library:
             python -m venv env
             .\env\Scripts\activate  # On Windows
             # or
             source env/bin/activate  # On macOS/Linux
             
             pip install --pre azure-ai-projects
        
    2. Creating an Agent with Tools:
      • Use the Azure AI Foundry service to set up your agent. If you're following the tutorial you mentioned, you can integrate the tools like the Deep Research tool or any custom tools you plan on creating.
      • Here's a quick example to create a tool and specify its integration:
             run = project_client.agents.runs.create_and_process(
        
      thread_id=thread.id, agent_id=agent.id, tool_choice={"type": "your_tool_type"} # specify your tool type here )
           
      
    • Make sure to replace "your_tool_type" with the specific type of tool you want to use, like the Deep Research tool.
    1. Define Instructions:
      • In your setup, clearly define the instructions for the model to guide it on how to utilize the tool. For instance:
             instructions = "Use the AI Search tool for product related information."
        
      • This helps the AI model understand the purpose and functionality of each tool.
    2. Testing and Validation:
      • Once your tool is created and integrated, test it thoroughly to ensure it's working as expected by running some sample queries or tasks.

    Additional Considerations

    • Permissions: Ensure you have the necessary RBAC permissions to create and manage tools within your Azure environment.
    • Optimal Token Allocation: Make sure your AI model has sufficient Tokens-Per-Minute (TPM) allocated. You might want at least 30k TPM for optimal performance.

    Here are some helpful links to the relevant documentation:

    I Hope this helps. Do let me know if you have any further queries.


    If this answers your query, please do click Accept Answer and Yes for was this answer helpful.

    Thank you!

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.