Skip to main content
This tutorial shows you how to use OpenAI’s function calling with Linkup to give your AI access to real-time web search results.
2

Install and Setup

Install the required packages:
pip install linkup-sdk openai
Set up your clients:
from openai import OpenAI
from linkup import LinkupClient
import json

# Initialize clients with your API keys
openai_client = OpenAI(api_key="your_openai_api_key")
linkup_client = LinkupClient(api_key="your_linkup_api_key")
3

Define the Function Schema

Tell OpenAI about your search function using the Responses API format:
tools = [{
    "type": "function",
    "name": "search_web",
    "description": "Search the web for current information. Returns comprehensive content from relevant sources.",
    "parameters": {
        "type": "object",
        "properties": {
            "query": {
                "type": "string",
                "description": "The search query"
            }
        },
        "required": ["query"]
    }
}]
4

Handle the Conversation

Put it all together using the Responses API:
# Create a running input list we will add to over time
input_list = [
    {"role": "user", "content": "What are the latest AI developments?"}
]

# Create a response with function calling capability
response = openai_client.responses.create(
    model="gpt-5",
    tools=tools,
    input=input_list,
)

# Save function call outputs for subsequent requests
input_list += response.output

# Check if OpenAI wants to call our function
for item in response.output:
    if item.type == "function_call":
        if item.name == "search_web":
            # Get the function call details
            args = json.loads(item.arguments)
            
            # Call Linkup to search the web
            linkup_response = linkup_client.search(
                query=args["query"],
                depth="standard",
                output_type="searchResults"
            )
            
            # Convert response to JSON format for OpenAI
            search_results_json = json.dumps(linkup_response.model_dump(), indent=2)
            
            # Provide function call results to the model
            input_list.append({
                "type": "function_call_output",
                "call_id": item.call_id,
                "output": search_results_json
            })

# Get final response with search results
final_response = openai_client.responses.create(
    model="gpt-5",
    tools=tools,
    input=input_list,
)

print(final_response.output_text)
If you prefer to use the traditional Chat Completions API, you’ll need a different tools format:
# Different tools format for Chat Completions API
chat_tools = [{
    "type": "function",
    "function": {
        "name": "search_web",
        "description": "Search the web for current information. Returns comprehensive content from relevant sources.",
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The search query"
                }
            },
            "required": ["query"]
        }
    }
}]

# Start a conversation
messages = [
    {"role": "user", "content": "What are the latest AI developments?"}
]

# Get OpenAI's response
response = openai_client.chat.completions.create(
    model="gpt-5",
    messages=messages,
    tools=chat_tools
)

# Check if OpenAI wants to call our function
if response.choices[0].message.tool_calls:
    # Get the function call details
    tool_call = response.choices[0].message.tool_calls[0]
    args = json.loads(tool_call.function.arguments)
    
    # Call Linkup to search the web
    linkup_response = linkup_client.search(
        query=args["query"],
        depth="standard",
        output_type="searchResults"
    )
    
    # Convert response to JSON format for OpenAI
    search_results_json = json.dumps(linkup_response.model_dump(), indent=2)
    
    # Add the function call and result to messages
    messages.append(response.choices[0].message)
    messages.append({
        "role": "tool",
        "name": "search_web",
        "content": search_results_json,
        "tool_call_id": tool_call.id
    })
    
    # Get final response with search results
    final_response = openai_client.chat.completions.create(
        model="gpt-5",
        messages=messages
    )
    
    print(final_response.choices[0].message.content)
else:
    print(response.choices[0].message.content)
I