@@ -92,7 +92,33 @@ def _convert_message_dict(
92
92
if function_call :
93
93
msg ["function_call" ] = function_call
94
94
else :
95
- msg ["content" ] = kwargs .get ("content" , "" )
95
+ content = kwargs .get ("content" )
96
+ if isinstance (content , list ):
97
+ tool_calls = []
98
+ content_parts = []
99
+ for item in content :
100
+ if item .get ("type" ) == "tool_use" :
101
+ tool_calls .append (
102
+ {
103
+ "id" : item .get ("id" ),
104
+ "type" : "function" ,
105
+ "function" : {
106
+ "name" : item .get ("name" ),
107
+ "arguments" : item .get ("input" ),
108
+ },
109
+ }
110
+ )
111
+ elif item .get ("type" ) == "text" :
112
+ content_parts .append (
113
+ {"type" : "text" , "text" : item .get ("text" )}
114
+ )
115
+
116
+ if tool_calls :
117
+ msg ["tool_calls" ] = tool_calls
118
+ if content_parts :
119
+ msg ["content" ] = content_parts # type: ignore
120
+ else :
121
+ msg ["content" ] = content # type: ignore
96
122
97
123
if tool_calls :
98
124
msg ["tool_calls" ] = tool_calls
@@ -123,7 +149,34 @@ def _convert_message(
123
149
if function_call :
124
150
msg ["function_call" ] = function_call
125
151
else :
126
- msg ["content" ] = message .content # type: ignore
152
+ if isinstance (message .content , list ):
153
+ tool_calls = []
154
+ content_parts = []
155
+ for item in message .content :
156
+ if isinstance (item , str ):
157
+ continue
158
+ if item .get ("type" ) == "tool_use" :
159
+ tool_calls .append (
160
+ {
161
+ "id" : item .get ("id" ),
162
+ "type" : "function" ,
163
+ "function" : {
164
+ "name" : item .get ("name" ),
165
+ "arguments" : item .get ("input" ),
166
+ },
167
+ }
168
+ )
169
+ elif item .get ("type" ) == "text" :
170
+ content_parts .append (
171
+ {"type" : "text" , "text" : item .get ("text" )}
172
+ )
173
+
174
+ if tool_calls :
175
+ msg ["tool_calls" ] = tool_calls
176
+ if content_parts :
177
+ msg ["content" ] = content_parts # type: ignore
178
+ else :
179
+ msg ["content" ] = message .content # type: ignore
127
180
128
181
if tool_calls :
129
182
msg ["tool_calls" ] = tool_calls
@@ -201,7 +254,12 @@ def _build_llm_settings(
201
254
{"type" : "function" , "function" : f } for f in settings ["functions" ]
202
255
]
203
256
if "tools" in settings :
204
- tools = settings ["tools" ]
257
+ tools = [
258
+ {"type" : "function" , "function" : t }
259
+ if t .get ("type" ) != "function"
260
+ else t
261
+ for t in settings ["tools" ]
262
+ ]
205
263
return provider , model , tools , settings
206
264
207
265
DEFAULT_TO_IGNORE = [
@@ -411,7 +469,9 @@ def _start_trace(self, run: Run) -> None:
411
469
)
412
470
step .tags = run .tags
413
471
step .metadata = run .metadata
414
- step .input = self .process_content (run .inputs )
472
+
473
+ if step .type != "llm" :
474
+ step .input = self .process_content (run .inputs )
415
475
416
476
self .steps [str (run .id )] = step
417
477
@@ -484,7 +544,6 @@ def _on_run_update(self, run: Run) -> None:
484
544
if v is not None
485
545
}
486
546
487
- current_step .output = message_completion
488
547
else :
489
548
completion_start = self .completion_generations [str (run .id )]
490
549
duration = time .time () - completion_start ["start" ]
@@ -509,7 +568,6 @@ def _on_run_update(self, run: Run) -> None:
509
568
output_token_count = usage_metadata .get ("output_tokens" ),
510
569
token_count = usage_metadata .get ("total_tokens" ),
511
570
)
512
- current_step .output = {"content" : completion }
513
571
514
572
if current_step :
515
573
if current_step .metadata is None :
@@ -521,7 +579,8 @@ def _on_run_update(self, run: Run) -> None:
521
579
outputs = run .outputs or {}
522
580
523
581
if current_step :
524
- current_step .output = self .process_content (outputs )
582
+ if current_step .type != "llm" :
583
+ current_step .output = self .process_content (outputs )
525
584
current_step .end ()
526
585
527
586
def _on_error (self , error : BaseException , * , run_id : "UUID" , ** kwargs : Any ):
0 commit comments