MCPcopy Index your code
hub / github.com/openai/openai-agents-python / ask_user_questions

Function ask_user_questions

examples/sandbox/tutorials/misc.py:168–237  ·  view source on GitHub ↗
(questions: list[Question])

Source from the content-addressed store, hash-verified

166
167
168def ask_user_questions(questions: list[Question]) -> list[QuestionAnswer]:
169 answers: list[QuestionAnswer] = []
170
171 for question_index, question in enumerate(questions, start=1):
172 suggested_options = [option.strip() for option in question.options if option.strip()]
173 custom_choice_index = len(suggested_options) + 1
174 options_text = Text.from_markup(
175 "\n".join(
176 [
177 *(
178 f"[cyan]{index}.[/cyan] {option}"
179 for index, option in enumerate(
180 suggested_options,
181 start=1,
182 )
183 ),
184 f"[cyan]{custom_choice_index}.[/cyan] Use your own text",
185 ]
186 )
187 )
188
189 console.print(
190 Panel(
191 Group(
192 Text(question.query),
193 options_text,
194 ),
195 title=f"Question {question_index}",
196 border_style="magenta",
197 box=box.ROUNDED,
198 expand=False,
199 )
200 )
201
202 while True:
203 choice = prompt_with_fallback(
204 f"[bold cyan]Select[/bold cyan] 1-{custom_choice_index}",
205 "1" if suggested_options else str(custom_choice_index),
206 )
207 if choice.isdigit() and 1 <= int(choice) <= len(suggested_options):
208 answer = suggested_options[int(choice) - 1]
209 break
210 if choice.isdigit() and int(choice) == custom_choice_index:
211 answer = prompt_with_fallback(
212 "[bold cyan]Your answer[/bold cyan]",
213 suggested_options[0] if suggested_options else "Use a conservative assumption.",
214 )
215 if answer:
216 break
217 continue
218 if choice and not choice.isdigit():
219 answer = choice
220 break
221
222 console.print(
223 f"[red]Please enter a number from 1 to {custom_choice_index}, or custom text.[/red]"
224 )
225

Callers

nothing calls this directly

Calls 5

GroupClass · 0.85
prompt_with_fallbackFunction · 0.85
QuestionAnswerClass · 0.85
appendMethod · 0.80
model_dumpMethod · 0.45

Tested by

no test coverage detected