()
| 12 | |
| 13 | |
| 14 | async def main(): |
| 15 | agent = Agent( |
| 16 | name="Code interpreter", |
| 17 | # Note: using gpt-5-class models with streaming for this tool may require org verification. |
| 18 | # Code interpreter does not support gpt-5 minimal reasoning effort; use default effort. |
| 19 | model="gpt-5.5", |
| 20 | instructions=( |
| 21 | "Always use the code interpreter tool to solve numeric problems, and show the code " |
| 22 | "you ran when possible." |
| 23 | ), |
| 24 | tools=[ |
| 25 | CodeInterpreterTool( |
| 26 | tool_config={"type": "code_interpreter", "container": {"type": "auto"}}, |
| 27 | ) |
| 28 | ], |
| 29 | ) |
| 30 | |
| 31 | with trace("Code interpreter example"): |
| 32 | print("Solving math problem with the code interpreter...") |
| 33 | result = Runner.run_streamed( |
| 34 | agent, |
| 35 | ( |
| 36 | "Use the code interpreter tool to calculate the square root of 273 * 312821 + " |
| 37 | "1782. Show the Python code you ran and then provide the numeric answer." |
| 38 | ), |
| 39 | ) |
| 40 | saw_code_interpreter_call = False |
| 41 | async for event in result.stream_events(): |
| 42 | if event.type != "run_item_stream_event": |
| 43 | continue |
| 44 | |
| 45 | item = event.item |
| 46 | if item.type == "tool_call_item": |
| 47 | raw_call = item.raw_item |
| 48 | if _get_field(raw_call, "type") == "code_interpreter_call": |
| 49 | saw_code_interpreter_call = True |
| 50 | code = _get_field(raw_call, "code") |
| 51 | if isinstance(code, str): |
| 52 | print(f"Code interpreter code:\n```\n{code}\n```\n") |
| 53 | continue |
| 54 | |
| 55 | print(f"Other event: {event.item.type}") |
| 56 | |
| 57 | if not saw_code_interpreter_call: |
| 58 | print("No code_interpreter_call item was emitted.") |
| 59 | print(f"Final output: {result.final_output}") |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
no test coverage detected