Repository Public
1#!/usr/bin/env python32# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project : API Atlas10# File : examples/openai/tools/tool-search/tool_search.py11# Description : Hosted tool search: defer a function inside a namespace;12# the model emits tool_search_call + tool_search_output,13# then calls it.14# Module : examples15# Repository : https://git.spboucher.ai/doc-api16# Created : 2026-09-1817# Last Modified : 2026-09-1818#19# =============================================================================20# AUTHOR & PROFESSIONAL AFFILIATION21# =============================================================================22#23# Author : Simon-Pierre Boucher24# Position : Professor of Finance25# Institution : Université du Québec en Outaouais (UQO)26# Department : Département des sciences administratives27# Organization : SP Boucher28#29# Website : https://spboucher.ai30# Git : https://git.spboucher.ai31#32# =============================================================================33# OFFICIAL CONTACT DIRECTORY34# =============================================================================35#36# General : info@spboucher.ai37# Professional : contact@spboucher.ai38# Direct : simon@spboucher.ai39# Research : research@spboucher.ai40# Development : dev@spboucher.ai41# Support : support@spboucher.ai42# Security/Admin : admin@spboucher.ai43# Notifications : notifications@spboucher.ai44#45# Preferred Contact: contact@spboucher.ai46#47# =============================================================================48# COPYRIGHT & INTELLECTUAL PROPERTY49# =============================================================================50#51# Copyright (c) 202652# Simon-Pierre Boucher53#54# All rights reserved where applicable.55#56# This file is governed by the applicable project license,57# copyright notices, institutional agreements and relevant law.58#59# Unless expressly authorized by the applicable license or60# rights holder, copying, redistribution, sublicensing,61# publication and commercial exploitation are not permitted.62#63# Existing third-party rights and notices remain unaffected.64#65# =============================================================================66# CONFIDENTIALITY & SECURITY67# =============================================================================68#69# Classification : Public70# Distribution : Public repository; use is governed by the project license71# Security Contact : admin@spboucher.ai72#73# Access to this source does not independently grant permission74# to redistribute or disclose confidential materials.75#76# Do not embed credentials, private keys, access tokens,77# certificates or confidential personal information.78#79# =============================================================================80# LICENSE & MAINTENANCE81# =============================================================================82#83# License : None specified (no license file in this repository)84# Maintainer : Simon-Pierre Boucher85# Technical Contact: dev@spboucher.ai86#87# =============================================================================88# END OF SP BOUCHER SOURCE HEADER89# =============================================================================9091"""Hosted tool search: defer a function inside a namespace; the model emits tool_search_call + tool_search_output, then calls it.92STATUS: LIVE_VERIFIED 2026-09-18 (gpt-5.4-mini — gpt-5.4-nano does not list tool_search on its model page).93Run: .venv/bin/python examples/openai/tools/tool-search/tool_search.py94"""95import sys96from pathlib import Path97ROOT = Path(__file__).resolve().parents[4]; sys.path.insert(0, str(ROOT))98from scripts.live import load_env, log_request; load_env()99from openai import OpenAI100client = OpenAI(); MODEL = "gpt-5.4-mini"101102deferred = {"type": "function", "name": "get_weather", "description": "Current weather for a city.", "defer_loading": True, "strict": True,103 "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], "additionalProperties": False}}104r = client.responses.create(model=MODEL, input="Weather in Paris? Find and use a tool.", max_output_tokens=96, reasoning={"effort": "low"},105 tools=[{"type": "tool_search"}, {"type": "namespace", "name": "weather", "description": "Weather lookup tools", "tools": [deferred]}])106log_request("openai", "POST", "/v1/responses", 200, 0.0007, "tool_search.py hosted")107for i in r.output:108 if i.type == "tool_search_call": print("tool_search_call:", i.execution, i.arguments)109 if i.type == "tool_search_output": print("tool_search_output:", [getattr(t, "name", t.type) for t in i.tools])110 if i.type == "function_call": print("function_call:", getattr(i, "namespace", None), i.name, i.arguments)111