Skip to content

Commit

Permalink
git: Handle None values in ChatMessage content (#422)
Browse files Browse the repository at this point in the history
* git: Handle None values in ChatMessage content

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Lucaz0619 and pre-commit-ci[bot] authored Feb 6, 2025
1 parent 54f1430 commit 38d7169
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/litserve/specs/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ResponseFormatJSONSchema(BaseModel):

class ChatMessage(BaseModel):
role: str
content: Union[str, List[Union[TextContent, ImageContent]]]
content: Optional[Union[str, List[Union[TextContent, ImageContent]]]] = None
name: Optional[str] = None
tool_calls: Optional[List[ToolCall]] = None
tool_call_id: Optional[str] = None
Expand Down Expand Up @@ -312,9 +312,10 @@ def validate_chat_message(self, obj):

def _encode_response(self, output: Union[Dict[str, str], List[Dict[str, str]]]) -> Dict:
logger.debug(output)
if isinstance(output, str):
if output is None:
message = {"role": "assistant", "content": None}
elif isinstance(output, str):
message = {"role": "assistant", "content": output}

elif self.validate_chat_message(output):
message = output
elif isinstance(output, dict) and "content" in output:
Expand Down Expand Up @@ -448,7 +449,7 @@ async def non_streaming_completion(self, request: ChatCompletionRequest, generat
if chat_msg.tool_calls:
tool_calls = chat_msg.tool_calls

content = "".join(msgs)
content = "".join(msg for msg in msgs if msg is not None)
msg = {"role": "assistant", "content": content, "tool_calls": tool_calls}
choice = ChatCompletionResponseChoice(index=i, message=msg, finish_reason="stop")
choices.append(choice)
Expand Down

0 comments on commit 38d7169

Please sign in to comment.