Skip to content

400 error when using saved prompt with built-in tool names in tool_choice #1304

@fathulfahmy

Description

@fathulfahmy

Describe the bug

When using Agents SDK with reusable prompt, tools defined in prompt is not found. Workaround is to define tool in source code.

Debug information

  • Agents SDK version: v0.2.3
  • Python version: Python 3.11

Repro steps

  1. Create a prompt in OpenAI dashboard.
Model
name: gpt-4.1-mini
text.format: text
tool_choice: web_search_preview
temp: 0.00

Tools
Web Search

System message
Search reputable web sources to verify and gather information about the customer's occupation and its typical stability, income potential, or risk level.

**Example:**

Reasoning:  
The customer is a software engineer, a profession typically associated with high job stability and above-average income [Bureau of Labor Statistics]. The technology industry has displayed consistent growth and demand for experienced developers [Indeed Hiring Lab]. 

References:  
- https://www.bls.gov/oes/current/oes151252.htm  
- https://www.hiringlab.org/2023/01/10/tech-job-trends/  
  1. Copy prompt id e.g. pmpt_1234567890.
  2. Attach prompt id to an agent.
    OPENAI_JOB_SEARCH_PROMPT_ID = "pmpt_123"

    job_search_agent = Agent(
        model="gpt-4.1-mini",
        name="job_searcher",
        prompt={"id": OPENAI_JOB_SEARCH_PROMPT_ID},
    )

    job_search_result = await Runner.run(job_search_agent, "Software Developer in Malaysia")

    return job_search_result.final_output


Error:

raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Tool choice 'web_search_preview' not found in 'tools' parameter.", 'type': 'invalid_request_error', 'param': 'tool_choice', 'code': None}}


Workaround:

    OPENAI_JOB_SEARCH_PROMPT_ID = "pmpt_123"

    job_search_agent = Agent(
        model="gpt-4.1-mini",
        name="job_searcher",
        prompt={"id": OPENAI_JOB_SEARCH_PROMPT_ID},
        tools=[
            WebSearchTool(
                search_context_size="high",
                user_location={
                    "type": "approximate",
                },
            )
        ],
    )

    job_search_result = await Runner.run(job_search_agent, "Software Developer in Malaysia")

    return job_search_result.final_output

Expected behavior

Tools to be managed and called using reusable prompt.

    OPENAI_JOB_SEARCH_PROMPT_ID = "pmpt_123"

    job_search_agent = Agent(
        model="gpt-4.1-mini",
        name="job_searcher",
        prompt={"id": OPENAI_JOB_SEARCH_PROMPT_ID},
    )

    job_search_result = await Runner.run(job_search_agent, "Software Developer in Malaysia")

    return job_search_result.final_output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions