Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agents.exceptions.UserError: Hosted tools are not supported with the ChatCompletions API. FGot tool type: <class 'agents.tool.WebSearchTool'>, tool: WebSearchTool(user_location={'type': 'approximate', 'city': '西安'}, search_context_size='medium') #194

Open
ai-srcflow opened this issue Mar 17, 2025 · 6 comments
Labels
question Question about using the SDK

Comments

@ai-srcflow
Copy link

Question

inner tools used error :

agents.exceptions.UserError: Hosted tools are not supported with the ChatCompletions API. FGot tool type: <class 'agents.tool.WebSearchTool'>, tool: WebSearchTool(user_location={'type': 'approximate', 'city': '西安'}, search_context_size='medium')

it seems ChatCompletions API compared Response API is incompatible,how to resolve this matter? thx

@ai-srcflow ai-srcflow added the question Question about using the SDK label Mar 17, 2025
@DanieleMorotti
Copy link

Hi, to use web search with the ChatCompletions API, you should adopt the specific model.
Both using the openai-agents package:

import asyncio
from agents import OpenAIChatCompletionsModel, AsyncOpenAI, Agent, Runner

async def main():
    agent = Agent(
        name="Assistant",
        instructions="You are a helpful assistant.",
        model=OpenAIChatCompletionsModel( 
            model="gpt-4o-search-preview-2025-03-11",
            openai_client=AsyncOpenAI()
        )
    )

    result = await Runner.run(agent, "What is the news in the world today?")
    print(result.final_output)

if __name__ == "__main__":
    asyncio.run(main())

or directly with the ChatCompletions API, as you can see on their website:

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o-search-preview",
    web_search_options={},
    messages=[
        {
            "role": "user",
            "content": "What was a positive news story from today?",
        }
    ],
)

print(completion.choices[0].message.content)

@hovofn
Copy link

hovofn commented Mar 17, 2025

from agents import set_default_openai_api

set_default_openai_api("chat_completions")

it will be use ChatCompletions API

@rm-openai
Copy link
Collaborator

Thank you @DanieleMorotti and @hovofn! More info here as well: https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat

@ai-srcflow
Copy link
Author

ai-srcflow commented Mar 18, 2025

my code :

import asyncio
from agents import Agent, Runner, WebSearchTool, trace, OpenAIChatCompletionsModel, set_default_openai_api
from openai import AsyncOpenAI

client = AsyncOpenAI(api_key="fxxxxx",base_url="https://api.360.cn/v1")
set_default_openai_api("chat_completions")

async def main():
agent = Agent(
name="Web searcher",
instructions="You are a helpful agent.",
model=OpenAIChatCompletionsModel(
openai_client=client,
model="gpt-4"
),
tools=[WebSearchTool(user_location={"type": "approximate", "city": "西安"})],
)

with trace("Web search example"):
    result = await Runner.run(
        agent,
        "search the web for 'local sports news' and give me 1 interesting update in a sentence.",
    )
    print(result.final_output)

if name == "main":
asyncio.run(main())

it looks like right ,but error always

Hosted tools are not supported with the ChatCompletions API. FGot tool type: <class 'agents.tool.WebSearchTool'>

why....

Image

@ai-srcflow
Copy link
Author

ai-srcflow commented Mar 18, 2025

the api and key to compute chatting with openai sdk is normal。only use inner tools is abnormal

Image

Image

@DanieleMorotti
Copy link

my code :

import asyncio from agents import Agent, Runner, WebSearchTool, trace, OpenAIChatCompletionsModel, set_default_openai_api from openai import AsyncOpenAI

client = AsyncOpenAI(api_key="fxxxxx",base_url="https://api.360.cn/v1") set_default_openai_api("chat_completions")

async def main(): agent = Agent( name="Web searcher", instructions="You are a helpful agent.", model=OpenAIChatCompletionsModel( openai_client=client, model="gpt-4" ), tools=[WebSearchTool(user_location={"type": "approximate", "city": "西安"})], )

with trace("Web search example"):
    result = await Runner.run(
        agent,
        "search the web for 'local sports news' and give me 1 interesting update in a sentence.",
    )
    print(result.final_output)

if name == "main": asyncio.run(main())

it looks like right ,but error always

Hosted tools are not supported with the ChatCompletions API. FGot tool type: <class 'agents.tool.WebSearchTool'>

why....

Image

Yes, you can't use WebSearchTool with ChatCompletionsAPI. If you check my previous example, you can see that you should use gpt-4o-search-preview or gpt-4o-mini-search-preview instead of passing the tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about using the SDK
Projects
None yet
Development

No branches or pull requests

4 participants