Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: Handle None values in ChatMessage content #422

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading