@@ -449,7 +449,11 @@ async def get_chat_completion(
449449 response_format : dict | None = None ,
450450 ) -> AsyncIterator [ChatCompletionChunk ]:
451451 context_variables = defaultdict (str , context_variables )
452- messages = Converter .items_to_messages (history )
452+ try :
453+ messages = Converter .items_to_messages (history )
454+ except Exception as exc :
455+ yield self .create_chunk (f"Failed to convert items to messages: { exc } " )
456+ return
453457
454458 if self .agent .system :
455459 messages .insert (
@@ -460,7 +464,12 @@ async def get_chat_completion(
460464 },
461465 )
462466
463- tools = [function_to_jsonschema (tool ) for tool in self .agent .tools ]
467+ try :
468+ tools = [function_to_jsonschema (tool ) for tool in self .agent .tools ]
469+ except Exception as exc :
470+ yield self .create_chunk (f"Failed to convert tools to JSON schema: { exc } " )
471+ return
472+
464473 # hide context_variables from model
465474 for tool in tools :
466475 params = tool ["function" ]["parameters" ]
@@ -492,20 +501,22 @@ async def get_chat_completion(
492501 except Exception as exc :
493502 # Return the error in form of a completion chunk.
494503 model = self .agent .model .id
495- chunk = ChatCompletionChunk (
496- id = FAKE_ID ,
497- choices = [
498- Choice (
499- delta = ChoiceDelta (
500- role = "assistant" ,
501- content = f"Failed to chat with { model } : { exc } " ,
502- ),
503- finish_reason = "stop" ,
504- index = 0 ,
505- )
506- ],
507- created = int (time .time ()),
508- model = model ,
509- object = "chat.completion.chunk" ,
510- )
511- yield chunk
504+ yield self .create_chunk (f"Failed to chat with { model } : { exc } " )
505+
506+ def create_chunk (self , content : str ) -> ChatCompletionChunk :
507+ return ChatCompletionChunk (
508+ id = FAKE_ID ,
509+ choices = [
510+ Choice (
511+ delta = ChoiceDelta (
512+ role = "assistant" ,
513+ content = content ,
514+ ),
515+ finish_reason = "stop" ,
516+ index = 0 ,
517+ )
518+ ],
519+ created = int (time .time ()),
520+ model = self .agent .model .id ,
521+ object = "chat.completion.chunk" ,
522+ )
0 commit comments