Examples and guides for using the OpenAI API
Add tool output formatting section to Realtime prompting guide with e… (#2460)
Minhajul Hoque5943a5d0096f
diff --git a/examples/Realtime_prompting_guide.ipynb b/examples/Realtime_prompting_guide.ipynbindex fa5dd790..e3322a09 100644--- a/examples/Realtime_prompting_guide.ipynb+++ b/examples/Realtime_prompting_guide.ipynb@@ -1320,6 +1320,66 @@"```"]},+ {+ "cell_type": "markdown",+ "id": "toolfmt01",+ "metadata": {},+ "source": [+ "## Tool Output Formatting\n",+ "Some tool outputs, especially long strings that must be repeated verbatim, can be out-of-distribution for the model. During training, tool outputs commonly look like JSON objects with named fields. If your tool returns a raw string and separately asks the model to “repeat exactly,” the model may be more prone to paraphrasing, truncation, or blending in its own preamble.\n",+ "\n",+ "A practical fix is to make the tool output look like a normal tool result and make the verbatim requirement machine-explicit.\n",+ "\n",+ "* **When to use:** A tool returns **long or complex structured content** (multi-sentence instructions, handoff packets, IDs/links, policy summaries, multi-step procedures, etc.) and you observe **truncation, paraphrasing, dropped fields, reordering, or the model blending in its own preamble/commentary**.\n",+ "\n",+ "* **What it does:** Wraps the tool output in a **small, explicit JSON envelope** (e.g., `response_text` plus flags like `require_repeat_verbatim`, `format`, or `content_type`) so the response looks more **in-distribution** and the expected realization behavior is **machine-clear**.\n",+ "\n",+ "* **How to adapt:** Keep the schema **minimal and stable**. Clearly document the expected tool output shape in both your **Tools instructions** and next to the **tool definition** (e.g., “If `require_repeat_verbatim` is true, output exactly `response_text` and nothing else,” or “Render `response_text` as-is; do not add, omit, or reorder fields from the tool output.”).\n",+ "\n"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "toolfmt02",+ "metadata": {},+ "source": [+ "### Examples\n",+ "\n",+ "#### Example: raw string (more error-prone)\n",+ "\n",+ "Tool returns:\n",+ "\n",+ "```python\n",+ "I just sent you an email with the verification link. Please open it and click “Confirm”.\n",+ "```\n",+ "\n",+ "Model sometimes says:\n",+ "\n",+ "- “I’ve emailed you a verification link…” (paraphrase)\n",+ "\n",+ "- Drops the last sentence (truncation)\n",+ "\n",+ "- Adds extra commentary (“Can I help with anything else?”)\n",+ "\n",+ "#### Example: wrapped JSON (more in-distribution, more reliable)\n",+ "Tool returns:\n",+ "\n",+ "```json\n",+ "{\n",+ " \"response_text\": \"I just sent you an email with the verification link. Please open it and click “Confirm”.\",\n",+ " \"require_repeat_verbatim\": true\n",+ "}\n",+ "```\n",+ "\n",+ "Because this looks like a typical tool result (JSON object), the model generally has an easier time:\n",+ "\n",+ "- recognizing what the “authoritative” content is (response_text)\n",+ "\n",+ "- understanding the realization constraint (require_repeat_verbatim)\n",+ "\n",+ "- reproducing the tool output cleanly, without truncation or extra commentary"+ ]+ },{"cell_type": "markdown","id": "65d35498",