Skip to content

Commit c22b73d

Browse files
committed
Add tool outputs
1 parent 8b1735d commit c22b73d

1 file changed

Lines changed: 56 additions & 8 deletions

File tree

src/agentex/lib/core/temporal/plugins/openai_agents/models/temporal_streaming_model.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,36 @@ async def get_response(
452452

453453
trace = self.tracer.trace(trace_id)
454454

455+
# Serialize input for span tracing
456+
span_input = {
457+
"model": self.model_name,
458+
"has_system_instructions": system_instructions is not None,
459+
"input_type": type(input).__name__,
460+
"tools_count": len(tools) if tools else 0,
461+
"handoffs_count": len(handoffs) if handoffs else 0,
462+
}
463+
464+
# Include actual input content for traceability
465+
try:
466+
if isinstance(input, str):
467+
span_input["input_content"] = input
468+
elif isinstance(input, list):
469+
# Serialize input items
470+
serialized_input = []
471+
for item in input:
472+
try:
473+
serialized_input.append(_serialize_item(item))
474+
except Exception:
475+
pass
476+
if serialized_input:
477+
span_input["input_items"] = serialized_input
478+
except Exception as e:
479+
logger.warning(f"Failed to serialize input for span: {e}")
480+
455481
async with trace.span(
456482
parent_id=parent_span_id,
457483
name="streaming_model_get_response",
458-
input={
459-
"model": self.model_name,
460-
"has_system_instructions": system_instructions is not None,
461-
"input_type": type(input).__name__,
462-
"tools_count": len(tools) if tools else 0,
463-
"handoffs_count": len(handoffs) if handoffs else 0,
464-
},
484+
input=span_input,
465485
) as span:
466486
# Always use Responses API for streaming
467487
if not task_id:
@@ -826,6 +846,8 @@ async def get_response(
826846
# Serialize response output items for span tracing
827847
new_items = []
828848
final_output = None
849+
tool_calls = []
850+
tool_outputs = []
829851

830852
for item in response_output:
831853
try:
@@ -845,12 +867,38 @@ async def get_response(
845867
logger.warning(f"Failed to serialize item in temporal_streaming_model: {e}")
846868
continue
847869

870+
# Extract tool calls and outputs from input
871+
try:
872+
if isinstance(input, list):
873+
for item in input:
874+
try:
875+
item_dict = _serialize_item(item) if not isinstance(item, dict) else item
876+
if item_dict:
877+
# Capture function calls
878+
if item_dict.get('type') == 'function_call':
879+
tool_calls.append(item_dict)
880+
# Capture function outputs
881+
elif item_dict.get('type') == 'function_call_output':
882+
tool_outputs.append(item_dict)
883+
except Exception:
884+
pass
885+
except Exception as e:
886+
logger.warning(f"Failed to extract tool calls and outputs: {e}")
887+
848888
# Set span output with structured data
849889
if span:
850-
span.output = {
890+
output_data = {
851891
"new_items": new_items,
852892
"final_output": final_output,
853893
}
894+
# Include tool calls if any were in the input
895+
if tool_calls:
896+
output_data["tool_calls"] = tool_calls
897+
# Include tool outputs if any were processed
898+
if tool_outputs:
899+
output_data["tool_outputs"] = tool_outputs
900+
901+
span.output = output_data
854902

855903
# Return the response
856904
return ModelResponse(

0 commit comments

Comments
 (0)