Skip to content

Examples and guides for using the OpenAI API

Enhance Realtime_eval_guide notebook with improved image formatting a… (#2420)

Minhajul Hoquefa82395d3682
diff --git a/examples/Realtime_eval_guide.ipynb b/examples/Realtime_eval_guide.ipynb
index aa58ba2f..04dba52c 100644
--- a/examples/Realtime_eval_guide.ipynb
+++ b/examples/Realtime_eval_guide.ipynb
@@ -13,11 +13,13 @@
"id": "fafa08e6",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
" width=\"650\"\n",
" src=\"../images/realtime_eval_page_01_image_01.png\"\n",
" alt=\"Diagram from page 1\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -66,11 +68,13 @@
"id": "89dd169b",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"700\"\n",
" src=\"../images/realtime_eval_page_02_image_01.png\"\n",
" alt=\"Diagram from page 2\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -84,7 +88,7 @@
"\n",
"- Content is correct but the experience is broken: audio gets chopped during barge-in because the interruption boundary is wrong.\n",
"\n",
- "- Answer is “right” but feels slow: latency came from network quality, turn detection slowness, not the model’s reasoning. You can learn more about the various events that the Realtime API triggers here.\n",
+ "- Answer is “right” but feels slow: latency came from network quality, turn detection slowness, not the model’s reasoning.\n",
"\n",
"You can learn more about the various events that the Realtime API triggers [here](https://platform.openai.com/docs/api-reference/realtime-server-events).\n",
"\n",
@@ -120,11 +124,13 @@
"id": "2ec20772",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
" width=\"650\"\n",
" src=\"../images/realtime_eval_page_03_image_01.png\"\n",
" alt=\"Diagram from page 3\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -154,11 +160,13 @@
"id": "48a6c62b",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1000\"\n",
" src=\"../images/realtime_eval_page_04_image_01.png\"\n",
" alt=\"Diagram from page 4\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -182,11 +190,13 @@
"id": "4427dd03",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1000\"\n",
" src=\"../images/realtime_eval_page_04_image_02.png\"\n",
" alt=\"Diagram from page 4\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -206,11 +216,13 @@
"id": "87a1e0a7",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1000\"\n",
" src=\"../images/realtime_eval_page_05_image_01.png\"\n",
" alt=\"Diagram from page 5\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -234,11 +246,13 @@
"id": "cec946d3",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1000\"\n",
" src=\"../images/realtime_eval_page_05_image_02.png\"\n",
" alt=\"Diagram from page 5\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -294,11 +308,13 @@
"id": "6a591cdb",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1200\"\n",
" src=\"../images/realtime_eval_page_07_image_01.png\"\n",
" alt=\"Diagram from page 7\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -316,11 +332,13 @@
"id": "68be4755",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
" width=\"650\"\n",
" src=\"../images/realtime_eval_page_07_image_02.png\"\n",
" alt=\"Diagram from page 7\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -347,11 +365,13 @@
"id": "785fa85a",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
" width=\"650\"\n",
" src=\"../images/realtime_eval_page_08_image_01.png\"\n",
" alt=\"Diagram from page 8\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -403,11 +423,13 @@
"id": "28efb845",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"900\"\n",
" src=\"../images/realtime_eval_page_09_image_01.png\"\n",
" alt=\"Diagram from page 9\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -433,11 +455,13 @@
"id": "65abb0c8",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1000\"\n",
" src=\"../images/realtime_eval_page_10_image_01.png\"\n",
" alt=\"Diagram from page 10\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -504,11 +528,13 @@
"id": "4eb150f0",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1000\"\n",
" src=\"../images/realtime_eval_page_10_image_02.png\"\n",
" alt=\"Diagram from page 10\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
@@ -547,11 +573,13 @@
"id": "ea242990",
"metadata": {},
"source": [
+ "<p align=\"center\">\n",
"<img\n",
- " width=\"650\"\n",
+ " width=\"1000\"\n",
" src=\"../images/realtime_eval_page_11_image_01.png\"\n",
" alt=\"Diagram from page 11\"\n",
- "/>\n"
+ "/>\n",
+ "</p>\n"
]
},
{
diff --git a/examples/evals/realtime_evals/run_harness/run_realtime_evals.py b/examples/evals/realtime_evals/run_harness/run_realtime_evals.py
index 07f539bf..2ca5cdcf 100644
--- a/examples/evals/realtime_evals/run_harness/run_realtime_evals.py
+++ b/examples/evals/realtime_evals/run_harness/run_realtime_evals.py
@@ -143,6 +143,39 @@ def compute_tool_outputs(
return outputs
+def build_tool_output_by_call_id(
+ tool_outputs: List[Dict[str, Any]],
+) -> Dict[str, Dict[str, Any]]:
+ outputs_by_call_id: Dict[str, Dict[str, Any]] = {}
+ for output in tool_outputs:
+ call_id = output.get("call_id", "")
+ if not call_id:
+ continue
+ outputs_by_call_id[call_id] = output
+ return outputs_by_call_id
+
+
+def transcript_lines_for_assistant_turn(
+ turn_index: int,
+ response_segments: List[Dict[str, Any]],
+ tool_outputs_by_call_id: Dict[str, Dict[str, Any]],
+) -> List[str]:
+ lines: List[str] = []
+ for segment in response_segments:
+ assistant_text = str(segment.get("assistant_text", "")).strip()
+ if assistant_text:
+ lines.append(f"TURN {turn_index} ASSISTANT: {assistant_text}")
+ for tool_call in segment.get("tool_calls", []):
+ lines.append(f"TURN {turn_index} TOOL_CALL: {json.dumps(tool_call)}")
+ call_id = str(tool_call.get("call_id", "")).strip()
+ if call_id and call_id in tool_outputs_by_call_id:
+ lines.append(
+ f"TURN {turn_index} TOOL_OUTPUT: "
+ f"{json.dumps(tool_outputs_by_call_id[call_id])}"
+ )
+ return lines
+
+
def build_simulator_prompt_text(
history: List[Dict[str, str]], fixed_first_turn: str, turn_index: int
) -> str:
@@ -370,6 +403,11 @@ async def run_simulation(
real_time = bool(audio_config.get("real_time", args.real_time))
max_turns = int(turns_config.get("max_turns", 0))
+ max_turns_override = simulation_row.get("max_turns_override")
+ if max_turns_override is not None and not pd.isna(max_turns_override):
+ override_value = int(max_turns_override)
+ if override_value > 0:
+ max_turns = override_value
if args.max_turns > 0:
max_turns = args.max_turns
if max_turns <= 0:
@@ -553,9 +591,11 @@ async def run_simulation(
)
assistant_text = assistant_result["assistant_text"]
+ assistant_segments = assistant_result.get("response_segments", [])
assistant_audio_bytes = assistant_result["output_audio_bytes"]
tool_calls = assistant_result["tool_calls"]
tool_outputs = compute_tool_outputs(tool_calls, tool_mocks)
+ tool_outputs_by_call_id = build_tool_output_by_call_id(tool_outputs)
tool_call_grade_data = compute_tool_call_grade(
expected_tool_name,
expected_tool_args_text,
@@ -578,20 +618,31 @@ async def run_simulation(
assistant_audio_path, assistant_audio_bytes, sample_rate_hz
)
- transcript_lines.append(
- f"TURN {turn_index} ASSISTANT: {assistant_text}"
+ transcript_lines.extend(
+ transcript_lines_for_assistant_turn(
+ turn_index, assistant_segments, tool_outputs_by_call_id
+ )
)
+ if not assistant_segments and assistant_text:
+ # Keep backwards-compatible transcript output if no segment metadata is available.
+ transcript_lines.append(
+ f"TURN {turn_index} ASSISTANT: {assistant_text}"
+ )
conversation_history.append(
{"role": "assistant", "text": assistant_text}
)
if tool_calls:
- tool_summary = (
- f"TURN {turn_index} TOOL_CALLS: {json.dumps(tool_calls)} "
- f"TOOL_OUTPUTS: {json.dumps(tool_outputs)}"
- )
- tool_summaries.append(tool_summary)
- transcript_lines.append(tool_summary)
+ for tool_call in tool_calls:
+ tool_summaries.append(
+ f"TURN {turn_index} TOOL_CALL: {json.dumps(tool_call)}"
+ )
+ call_id = str(tool_call.get("call_id", "")).strip()
+ if call_id and call_id in tool_outputs_by_call_id:
+ tool_summaries.append(
+ f"TURN {turn_index} TOOL_OUTPUT: "
+ f"{json.dumps(tool_outputs_by_call_id[call_id])}"
+ )
turn_context = build_turn_context(
simulation_id,
@@ -641,9 +692,6 @@ async def run_simulation(
for grader in turn_level_graders:
if grader.get("type") != "llm_as_judge":
continue
- if not tool_calls:
- # Skip judge calls when the assistant made no tool call.
- continue
turn_grade_requests.append(
{
"simulation_id": simulation_id,
diff --git a/examples/evals/realtime_evals/shared/realtime_harness_utils.py b/examples/evals/realtime_evals/shared/realtime_harness_utils.py
index e6b2e159..83e98644 100644
--- a/examples/evals/realtime_evals/shared/realtime_harness_utils.py
+++ b/examples/evals/realtime_evals/shared/realtime_harness_utils.py
@@ -7,6 +7,7 @@ import wave
from pathlib import Path
from typing import Any, Dict, List, Optional, TextIO
+from shared.graders import parse_json_dict
from shared.realtime_utils import ToolCallAccumulator
from shared.trace_utils import build_event_record
@@ -113,15 +114,22 @@ async def collect_realtime_response(
) -> Dict[str, Any]:
"""Collect a realtime response and optionally log each event as JSONL."""
assistant_text_parts: List[str] = []
+ current_response_text_parts: List[str] = []
output_audio_bytes = bytearray()
tool_call_accumulator = ToolCallAccumulator()
# Track tool calls we've already responded to so follow-up responses are clean.
responded_call_ids: set[str] = set()
+ response_segments: List[Dict[str, Any]] = []
first_audio_time_ms: Optional[float] = None
first_text_time_ms: Optional[float] = None
response_done_time_ms: Optional[float] = None
- usage_data: Dict[str, Any] = {}
+ usage_output_tokens = 0
+ usage_output_audio_tokens = 0
+ usage_output_text_tokens = 0
+ has_output_tokens = False
+ has_output_audio_tokens = False
+ has_output_text_tokens = False
awaiting_followup = False
loop = asyncio.get_running_loop()
@@ -160,18 +168,60 @@ async def collect_realtime_response(
if event_type == "response.output_audio_transcript.delta":
if first_text_time_ms is None:
first_text_time_ms = event_time_ms
- assistant_text_parts.append(payload.get("delta", ""))
+ delta_text = payload.get("delta", "")
+ assistant_text_parts.append(delta_text)
+ current_response_text_parts.append(delta_text)
if event_type == "response.output_text.delta":
if first_text_time_ms is None:
first_text_time_ms = event_time_ms
- assistant_text_parts.append(payload.get("delta", ""))
+ delta_text = payload.get("delta", "")
+ assistant_text_parts.append(delta_text)
+ current_response_text_parts.append(delta_text)
if event_type == "response.done":
response_done_time_ms = event_time_ms
tool_call_accumulator.handle_event_payload(payload)
response = payload.get("response") or {}
- usage_data = response.get("usage") or {}
+ response_usage = response.get("usage") or {}
+ response_output_tokens = response_usage.get("output_tokens")
+ if response_output_tokens is not None:
+ usage_output_tokens += int(response_output_tokens)
+ has_output_tokens = True
+
+ response_output_details = response_usage.get("output_token_details") or {}
+ response_audio_tokens = response_output_details.get("audio_tokens")
+ if response_audio_tokens is not None:
+ usage_output_audio_tokens += int(response_audio_tokens)
+ has_output_audio_tokens = True
+ response_text_tokens = response_output_details.get("text_tokens")
+ if response_text_tokens is not None:
+ usage_output_text_tokens += int(response_text_tokens)
+ has_output_text_tokens = True
+
+ response_tool_calls: List[Dict[str, Any]] = []
+ for output_item in response.get("output") or []:
+ if output_item.get("type") != "function_call":
+ continue
+ call_id = output_item.get("call_id") or output_item.get("id") or ""
+ if not call_id:
+ continue
+ response_tool_calls.append(
+ {
+ "name": output_item.get("name", ""),
+ "arguments": parse_json_dict(output_item.get("arguments", "")),
+ "raw_arguments": output_item.get("arguments", "") or "",
+ "call_id": call_id,
+ }
+ )
+
+ response_segments.append(
+ {
+ "assistant_text": "".join(current_response_text_parts).strip(),
+ "tool_calls": response_tool_calls,
+ }
+ )
+ current_response_text_parts = []
tool_calls = tool_call_accumulator.build_tool_calls()
# Only respond to newly observed tool calls; some calls may repeat in follow-ups.
@@ -209,8 +259,18 @@ async def collect_realtime_response(
break
tool_calls = tool_call_accumulator.build_tool_calls()
+ usage_data: Dict[str, Any] = {}
+ if has_output_tokens:
+ usage_data["output_tokens"] = usage_output_tokens
+ if has_output_audio_tokens or has_output_text_tokens:
+ usage_data["output_token_details"] = {}
+ if has_output_audio_tokens:
+ usage_data["output_token_details"]["audio_tokens"] = usage_output_audio_tokens
+ if has_output_text_tokens:
+ usage_data["output_token_details"]["text_tokens"] = usage_output_text_tokens
return {
"assistant_text": "".join(assistant_text_parts).strip(),
+ "response_segments": response_segments,
"output_audio_bytes": bytes(output_audio_bytes),
"tool_calls": tool_calls,
"first_audio_time_ms": first_audio_time_ms,