Conditional structured output

What’s the right way to ask the Responses API for structured output conditionally?
In my case, if the call involves the web search tool, I want raw results but if there’s no web search, I want structured output. Example:

response = client.responses.parse(
model=“gpt-4o”,
input=messages,
tools=tools_list if tools_list else None,
text_format=BudgetItemsResponse
)

I guess the conditional would be in python, not the LLM since structured responses use client.responses.parse whereas non-structured use client.responses.create (unless I’m thinking about it wrong).

AFAIK the parameter is fixed, not conditional.
What you could try is to use a default text response first.

Then, if there is no web search usage you follow up with another prompt, using your previous_response_id and this time you enforce structured output and ask “put my previous answer in this format”.

Thanks.

Maybe I’ll go up a level and try this with the Agent SDK. I’m wondering if the triage agent is the right place for this kind of logic

There is a shortcut, which is to create new function/tool to do analysis (put every logic, rules, etc there ) then ask it to do analysis before parsing. The result of the analysis determines the parsing.

(input) → (analysis function) → (parsing function)

can you point me to an example?

I made a Triage endpoint with structured output, “web_search” or “standard” using 4.1 Nano. Every user message from the chat is triaged and fires against the correct endpoint.

Let say we have two options:
option1: [web_search] {/* query: string /}
option2: [none_web_search] {/
title: string, text: string */}

What I mean is that you would use another function to determine the type of the tools.

[analysis_function] {/* function_name: ‘tool_analyzer’, selection_justification: string, tool_name: ‘search_query_builder|article_structure’, result: ‘search_query_builder|article_structure’ */} The result of this will determine the next action whether is web_search or none_web_search, then use this result. This may also lower the cost if you have more functions but it might add extra latency.

If there are issues with reliability, latency, or other issues, Please keep this post updated with your scenario, I will add other options that might work.