Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

/cookbook: tool loop feeds reasoning items back (real-API fix) + cleared execution counts

nirholas0759f2eb4166
diff --git a/public/cookbook/index.html b/public/cookbook/index.html
index f39d124a8..7d10c09b4 100644
--- a/public/cookbook/index.html
+++ b/public/cookbook/index.html
@@ -7803,7 +7803,7 @@ a.anchor-link {
<meta property="og:title" content="Cookbook: self-correcting 3D with function calling · three.ws">
<meta property="og:description" content="An executable tutorial: an AI art director turns a one-line brief into a themed set of real 3D props with parallel function calls, inspects each render with vision, and rebuilds what fails. Read it with interactive galleries, or download the notebook and run it.">
<meta property="og:url" content="https://three.ws/cookbook">
- <meta property="og:image" content="https://three.ws/api/page-og?s=learn&amp;t=Cookbook%3A+self-correcting+3D+with+function+calling&amp;d=An+executable+tutorial%3A+an+AI+art+director+turns+a+one-line+brief+into+a+themed+set+of+real+3D+props+with+parallel+function+calls%2C+inspects+each+render+with+vis&amp;p=%2Fcookbook">
+ <meta property="og:image" content="https://three.ws/api/page-og?s=learn&amp;t=Cookbook%3A+self-correcting+3D+with+function+calling&amp;d=An+executable+tutorial%3A+an+AI+art+director+turns+a+one-line+brief+into+a+themed+set+of+real+3D+props+with+parallel+function+calls%2C+inspects+each+render+with&amp;p=%2Fcookbook">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:image:type" content="image/png">
@@ -7812,8 +7812,8 @@ a.anchor-link {
<meta name="twitter:site" content="@trythreews">
<meta name="twitter:title" content="Cookbook: self-correcting 3D with function calling · three.ws">
<meta name="twitter:description" content="An executable tutorial: an AI art director turns a one-line brief into a themed set of real 3D props with parallel function calls, inspects each render with vision, and rebuilds what fails. Read it with interactive galleries, or download the notebook and run it.">
- <meta name="twitter:image" content="https://three.ws/api/page-og?s=learn&amp;t=Cookbook%3A+self-correcting+3D+with+function+calling&amp;d=An+executable+tutorial%3A+an+AI+art+director+turns+a+one-line+brief+into+a+themed+set+of+real+3D+props+with+parallel+function+calls%2C+inspects+each+render+with+vis&amp;p=%2Fcookbook">
- <script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"WebPage","name":"Cookbook: self-correcting 3D with function calling","description":"An executable tutorial: an AI art director turns a one-line brief into a themed set of real 3D props with parallel function calls, inspects each render with vision, and rebuilds what fails. Read it with interactive galleries, or download the notebook and run it.","url":"https://three.ws/cookbook","isPartOf":{"@type":"WebSite","name":"three.ws","url":"https://three.ws"},"primaryImageOfPage":"https://three.ws/api/page-og?s=learn&t=Cookbook%3A+self-correcting+3D+with+function+calling&d=An+executable+tutorial%3A+an+AI+art+director+turns+a+one-line+brief+into+a+themed+set+of+real+3D+props+with+parallel+function+calls%2C+inspects+each+render+with+vis&p=%2Fcookbook"}]}</script>
+ <meta name="twitter:image" content="https://three.ws/api/page-og?s=learn&amp;t=Cookbook%3A+self-correcting+3D+with+function+calling&amp;d=An+executable+tutorial%3A+an+AI+art+director+turns+a+one-line+brief+into+a+themed+set+of+real+3D+props+with+parallel+function+calls%2C+inspects+each+render+with&amp;p=%2Fcookbook">
+ <script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"WebPage","name":"Cookbook: self-correcting 3D with function calling","description":"An executable tutorial: an AI art director turns a one-line brief into a themed set of real 3D props with parallel function calls, inspects each render with vision, and rebuilds what fails. Read it with interactive galleries, or download the notebook and run it.","url":"https://three.ws/cookbook","isPartOf":{"@type":"WebSite","name":"three.ws","url":"https://three.ws"},"primaryImageOfPage":"https://three.ws/api/page-og?s=learn&t=Cookbook%3A+self-correcting+3D+with+function+calling&d=An+executable+tutorial%3A+an+AI+art+director+turns+a+one-line+brief+into+a+themed+set+of+real+3D+props+with+parallel+function+calls%2C+inspects+each+render+with&p=%2Fcookbook"}]}</script>
</head>
<body class="jp-Notebook" data-jp-theme-light="true" data-jp-theme-name="JupyterLab Light">
<div id="tws-cookbook-bar" style="position:sticky;top:0;z-index:9999;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:10px 18px;background:#0b0d12;border-bottom:1px solid #232838;font-family:ui-sans-serif,-apple-system,'Segoe UI',Roboto,sans-serif;">
@@ -7907,7 +7907,7 @@ free.</li>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [1]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">pip</span> install --quiet --upgrade openai requests pillow
@@ -7943,7 +7943,7 @@ free.</li>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [2]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">base64</span>
@@ -8007,7 +8007,7 @@ real function the model will call, no mocking.</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [3]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">GenerationError</span><span class="p">(</span><span class="ne">RuntimeError</span><span class="p">):</span>
@@ -8251,7 +8251,7 @@ the batch. Every model below was generated live by this cell.</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [5]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">PROPS</span> <span class="o">=</span> <span class="p">[</span>
@@ -8392,7 +8392,7 @@ that must be a single, clear subject (the draft lane models one subject best).</
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [6]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">tools</span> <span class="o">=</span> <span class="p">[</span>
@@ -8440,8 +8440,13 @@ the model emits <strong>several</strong> <code>function_call</code> items in a s
prop), so we run them concurrently.</p>
<ol>
<li>Send the conversation plus the tool definitions.</li>
-<li>For every <code>function_call</code> the model returns this turn, run the real function
-(all of them in parallel) and append a <code>function_call_output</code> for each.</li>
+<li>Append the model's entire <code>output</code> to the conversation. Reasoning models
+pair each <code>function_call</code> with a <code>reasoning</code> item, and the API rejects a
+follow-up turn that has one without the other, so append everything rather
+than cherry-picking the calls.</li>
+<li>For every <code>function_call</code> the model returned this turn, run the real
+function (all of them in parallel) and append a <code>function_call_output</code> for
+each.</li>
<li>Send the updated conversation back so the model can react, call more tools,
or write its final summary.</li>
<li>Repeat until the model stops calling tools.</li>
@@ -8456,7 +8461,7 @@ back to the model instead of crashing the run.</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [7]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">ART_DIRECTOR</span> <span class="o">=</span> <span class="p">(</span>
@@ -8494,6 +8499,13 @@ back to the model instead of crashing the run.</p>
<span class="n">result</span><span class="p">,</span> <span class="n">payload</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="p">{</span><span class="s2">"status"</span><span class="p">:</span> <span class="s2">"error"</span><span class="p">,</span> <span class="s2">"error"</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)}</span>
<span class="k">return</span> <span class="n">call</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">payload</span>
+ <span class="c1"># Feed the model's ENTIRE output back first. Reasoning models emit</span>
+ <span class="c1"># reasoning items paired with their function calls, and the API</span>
+ <span class="c1"># rejects a follow-up turn that carries a function_call without its</span>
+ <span class="c1"># reasoning item, so append response.output wholesale rather than</span>
+ <span class="c1"># cherry-picking the calls.</span>
+ <span class="n">conversation</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">output</span><span class="p">)</span>
+
<span class="c1"># Run every tool call from this turn concurrently.</span>
<span class="k">with</span> <span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span> <span class="k">as</span> <span class="n">pool</span><span class="p">:</span>
<span class="k">for</span> <span class="n">call</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">payload</span> <span class="ow">in</span> <span class="n">pool</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">run_call</span><span class="p">,</span> <span class="n">calls</span><span class="p">):</span>
@@ -8501,7 +8513,6 @@ back to the model instead of crashing the run.</p>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">" </span><span class="si">{</span><span class="n">flag</span><span class="si">:</span><span class="s2">&lt;6</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">args</span><span class="p">[</span><span class="s1">'name'</span><span class="p">]</span><span class="si">:</span><span class="s2">&lt;16</span><span class="si">}</span><span class="s2"> &lt;- </span><span class="si">{</span><span class="n">args</span><span class="p">[</span><span class="s1">'prompt'</span><span class="p">]</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">if</span> <span class="n">result</span><span class="p">:</span>
<span class="n">gallery</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
- <span class="n">conversation</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">call</span><span class="p">)</span>
<span class="n">conversation</span><span class="o">.</span><span class="n">append</span><span class="p">({</span>
<span class="s2">"type"</span><span class="p">:</span> <span class="s2">"function_call_output"</span><span class="p">,</span>
<span class="s2">"call_id"</span><span class="p">:</span> <span class="n">call</span><span class="o">.</span><span class="n">call_id</span><span class="p">,</span>
@@ -8532,7 +8543,7 @@ own brief in the last line:</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [8]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">WINDOWSILL_BRIEF</span> <span class="o">=</span> <span class="p">(</span>
@@ -8727,7 +8738,7 @@ output against intent, and the grade is data.</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [9]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">snapshot</span><span class="p">(</span><span class="n">glb_url</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">512</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
@@ -8860,7 +8871,7 @@ survive script-free views too.</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [10]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">_QA_CSS</span> <span class="o">=</span> <span class="s2">"""</span>
@@ -9076,7 +9087,7 @@ human.</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [11]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">repair</span><span class="p">(</span><span class="n">inspected</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">theme</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
@@ -9238,7 +9249,7 @@ the cell to copy into your own project.</p>
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
-<div class="jp-InputPrompt jp-InputArea-prompt">In [12]:</div>
+<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">build_set</span><span class="p">(</span><span class="n">brief</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
diff --git a/public/cookbook/text_to_3d_with_function_calling.ipynb b/public/cookbook/text_to_3d_with_function_calling.ipynb
index 1c4860efe..a980c5262 100644
--- a/public/cookbook/text_to_3d_with_function_calling.ipynb
+++ b/public/cookbook/text_to_3d_with_function_calling.ipynb
@@ -646,11 +646,16 @@
"prop), so we run them concurrently.\n",
"\n",
"1. Send the conversation plus the tool definitions.\n",
- "2. For every `function_call` the model returns this turn, run the real function\n",
- " (all of them in parallel) and append a `function_call_output` for each.\n",
- "3. Send the updated conversation back so the model can react, call more tools,\n",
+ "2. Append the model's entire `output` to the conversation. Reasoning models\n",
+ " pair each `function_call` with a `reasoning` item, and the API rejects a\n",
+ " follow-up turn that has one without the other, so append everything rather\n",
+ " than cherry-picking the calls.\n",
+ "3. For every `function_call` the model returned this turn, run the real\n",
+ " function (all of them in parallel) and append a `function_call_output` for\n",
+ " each.\n",
+ "4. Send the updated conversation back so the model can react, call more tools,\n",
" or write its final summary.\n",
- "4. Repeat until the model stops calling tools.\n",
+ "5. Repeat until the model stops calling tools.\n",
"\n",
"Each tool call is wrapped so that a single failed generation reports its error\n",
"back to the model instead of crashing the run."
@@ -705,6 +710,13 @@
" result, payload = None, {\"status\": \"error\", \"error\": str(err)}\n",
" return call, args, result, payload\n",
"\n",
+ " # Feed the model's ENTIRE output back first. Reasoning models emit\n",
+ " # reasoning items paired with their function calls, and the API\n",
+ " # rejects a follow-up turn that carries a function_call without its\n",
+ " # reasoning item, so append response.output wholesale rather than\n",
+ " # cherry-picking the calls.\n",
+ " conversation.extend(response.output)\n",
+ "\n",
" # Run every tool call from this turn concurrently.\n",
" with ThreadPoolExecutor(max_workers=6) as pool:\n",
" for call, args, result, payload in pool.map(run_call, calls):\n",
@@ -712,7 +724,6 @@
" print(f\" {flag:<6} {args['name']:<16} <- {args['prompt']}\")\n",
" if result:\n",
" gallery.append(result)\n",
- " conversation.append(call)\n",
" conversation.append({\n",
" \"type\": \"function_call_output\",\n",
" \"call_id\": call.call_id,\n",