Skip to content

Commit 31d1cfc

Browse files
Polish the python code and browser experience (#198)
* fix: make python tool execution more robust * add tool input/output to python * add code interpreter output * improve web search compatibility and reasoning IDs
1 parent 7f7a545 commit 31d1cfc

File tree

5 files changed

+332
-34
lines changed

5 files changed

+332
-34
lines changed

examples/streamlit/streamlit_chat.py

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def run(container):
134134
)
135135

136136
text_delta = ""
137+
code_interpreter_sessions: dict[str, dict] = {}
137138

138139
_current_output_index = 0
139140
for line in response.iter_lines(decode_unicode=True):
@@ -166,8 +167,25 @@ def run(container):
166167
)
167168
placeholder = output.empty()
168169
elif output_type == "code_interpreter_call":
169-
output = container.chat_message("code_interpreter_call", avatar="🧪")
170-
placeholder = output.empty()
170+
item = data.get("item", {})
171+
item_id = item.get("id")
172+
message_container = container.chat_message(
173+
"code_interpreter_call", avatar="🧪"
174+
)
175+
status_placeholder = message_container.empty()
176+
code_placeholder = message_container.empty()
177+
outputs_container = message_container.container()
178+
code_text = item.get("code") or ""
179+
if code_text:
180+
code_placeholder.code(code_text, language="python")
181+
code_interpreter_sessions[item_id] = {
182+
"status": status_placeholder,
183+
"code": code_placeholder,
184+
"outputs": outputs_container,
185+
"code_text": code_text,
186+
"rendered_outputs": False,
187+
}
188+
placeholder = status_placeholder
171189
text_delta = ""
172190
elif event_type == "response.reasoning_text.delta":
173191
output.avatar = "🤔"
@@ -186,17 +204,77 @@ def run(container):
186204
if item.get("type") == "web_search_call":
187205
placeholder.markdown("✅ Done")
188206
if item.get("type") == "code_interpreter_call":
189-
placeholder.markdown("✅ Done")
207+
item_id = item.get("id")
208+
session = code_interpreter_sessions.get(item_id)
209+
if session:
210+
session["status"].markdown("✅ Done")
211+
final_code = item.get("code") or session["code_text"]
212+
if final_code:
213+
session["code"].code(final_code, language="python")
214+
session["code_text"] = final_code
215+
outputs = item.get("outputs") or []
216+
if outputs and not session["rendered_outputs"]:
217+
with session["outputs"]:
218+
st.markdown("**Outputs**")
219+
for output_item in outputs:
220+
output_type = output_item.get("type")
221+
if output_type == "logs":
222+
st.code(
223+
output_item.get("logs", ""),
224+
language="text",
225+
)
226+
elif output_type == "image":
227+
st.image(
228+
output_item.get("url", ""),
229+
caption="Code interpreter image",
230+
)
231+
session["rendered_outputs"] = True
232+
elif not outputs and not session["rendered_outputs"]:
233+
with session["outputs"]:
234+
st.caption("(No outputs)")
235+
session["rendered_outputs"] = True
236+
else:
237+
placeholder.markdown("✅ Done")
190238
elif event_type == "response.code_interpreter_call.in_progress":
191-
try:
192-
placeholder.markdown("⏳ Running")
193-
except Exception:
194-
pass
239+
item_id = data.get("item_id")
240+
session = code_interpreter_sessions.get(item_id)
241+
if session:
242+
session["status"].markdown("⏳ Running")
243+
else:
244+
try:
245+
placeholder.markdown("⏳ Running")
246+
except Exception:
247+
pass
248+
elif event_type == "response.code_interpreter_call.interpreting":
249+
item_id = data.get("item_id")
250+
session = code_interpreter_sessions.get(item_id)
251+
if session:
252+
session["status"].markdown("🧮 Interpreting")
195253
elif event_type == "response.code_interpreter_call.completed":
196-
try:
197-
placeholder.markdown("✅ Done")
198-
except Exception:
199-
pass
254+
item_id = data.get("item_id")
255+
session = code_interpreter_sessions.get(item_id)
256+
if session:
257+
session["status"].markdown("✅ Done")
258+
else:
259+
try:
260+
placeholder.markdown("✅ Done")
261+
except Exception:
262+
pass
263+
elif event_type == "response.code_interpreter_call_code.delta":
264+
item_id = data.get("item_id")
265+
session = code_interpreter_sessions.get(item_id)
266+
if session:
267+
session["code_text"] += data.get("delta", "")
268+
if session["code_text"].strip():
269+
session["code"].code(session["code_text"], language="python")
270+
elif event_type == "response.code_interpreter_call_code.done":
271+
item_id = data.get("item_id")
272+
session = code_interpreter_sessions.get(item_id)
273+
if session:
274+
final_code = data.get("code") or session["code_text"]
275+
session["code_text"] = final_code
276+
if final_code:
277+
session["code"].code(final_code, language="python")
200278
elif event_type == "response.completed":
201279
response = data.get("response", {})
202280
if debug_mode:

0 commit comments

Comments
 (0)