Skip to content

Commit 4cfb5ae

Browse files
committed
support both dict and object processing for response api's _process_response
1 parent 5d8b080 commit 4cfb5ae

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

dspy/clients/base_lm.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,31 @@ def _process_response(self, response):
191191
Returns:
192192
List of processed outputs
193193
"""
194+
def _to_dict(obj):
195+
"""Convert object-like values into plain dicts when possible."""
196+
if obj is None:
197+
return None
198+
if isinstance(obj, dict):
199+
return obj
200+
if hasattr(obj, "model_dump"):
201+
return obj.model_dump()
202+
if hasattr(obj, "__dict__") and not isinstance(obj, dict):
203+
return {k: v for k, v in obj.__dict__.items() if not k.startswith("_")}
204+
return obj
205+
194206
outputs = []
195207
tool_calls = []
196208
for output_item in response.output:
197-
if output_item.type == "message":
198-
for content_item in output_item.content:
199-
outputs.append(content_item.text)
200-
elif output_item.type == "function_call":
201-
tool_calls.append(output_item.model_dump())
202-
209+
output_dict = _to_dict(output_item)
210+
item_type = output_dict.get("type")
211+
if item_type == "message":
212+
content = output_dict.get("content", [])
213+
for content_item in content:
214+
content_dict = _to_dict(content_item)
215+
text = content_dict.get("text")
216+
outputs.append(text)
217+
elif item_type == "function_call":
218+
tool_calls.append(output_dict)
203219
if tool_calls:
204220
outputs.append({"tool_calls": tool_calls})
205221
return outputs

0 commit comments

Comments
 (0)