MCPcopy Create free account
hub / github.com/openai/openai-agents-python / main

Function main

examples/tools/code_interpreter.py:14–59  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12
13
14async 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
62if __name__ == "__main__":

Callers 1

Calls 6

AgentClass · 0.90
CodeInterpreterToolClass · 0.90
traceFunction · 0.90
_get_fieldFunction · 0.70
run_streamedMethod · 0.45
stream_eventsMethod · 0.45

Tested by

no test coverage detected