from openai import OpenAIfrom linkup import LinkupClientimport json# Initialize clients with your API keysopenai_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:
Copy
Ask AI
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:
Copy
Ask AI
# Create a running input list we will add to over timeinput_list = [ {"role": "user", "content": "What are the latest AI developments?"}]# Create a response with function calling capabilityresponse = openai_client.responses.create( model="gpt-5", tools=tools, input=input_list,)# Save function call outputs for subsequent requestsinput_list += response.output# Check if OpenAI wants to call our functionfor 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 resultsfinal_response = openai_client.responses.create( model="gpt-5", tools=tools, input=input_list,)print(final_response.output_text)
Using Chat Completions API (Alternative)
If you prefer to use the traditional Chat Completions API, you’ll need a different tools format:
Copy
Ask AI
# Different tools format for Chat Completions APIchat_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 conversationmessages = [ {"role": "user", "content": "What are the latest AI developments?"}]# Get OpenAI's responseresponse = openai_client.chat.completions.create( model="gpt-5", messages=messages, tools=chat_tools)# Check if OpenAI wants to call our functionif 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)