Skip to content

Commit 38d7169

Browse files
git: Handle None values in ChatMessage content (#422)
* 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>
1 parent 54f1430 commit 38d7169

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/litserve/specs/openai.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ResponseFormatJSONSchema(BaseModel):
133133

134134
class ChatMessage(BaseModel):
135135
role: str
136-
content: Union[str, List[Union[TextContent, ImageContent]]]
136+
content: Optional[Union[str, List[Union[TextContent, ImageContent]]]] = None
137137
name: Optional[str] = None
138138
tool_calls: Optional[List[ToolCall]] = None
139139
tool_call_id: Optional[str] = None
@@ -312,9 +312,10 @@ def validate_chat_message(self, obj):
312312

313313
def _encode_response(self, output: Union[Dict[str, str], List[Dict[str, str]]]) -> Dict:
314314
logger.debug(output)
315-
if isinstance(output, str):
315+
if output is None:
316+
message = {"role": "assistant", "content": None}
317+
elif isinstance(output, str):
316318
message = {"role": "assistant", "content": output}
317-
318319
elif self.validate_chat_message(output):
319320
message = output
320321
elif isinstance(output, dict) and "content" in output:
@@ -448,7 +449,7 @@ async def non_streaming_completion(self, request: ChatCompletionRequest, generat
448449
if chat_msg.tool_calls:
449450
tool_calls = chat_msg.tool_calls
450451

451-
content = "".join(msgs)
452+
content = "".join(msg for msg in msgs if msg is not None)
452453
msg = {"role": "assistant", "content": content, "tool_calls": tool_calls}
453454
choice = ChatCompletionResponseChoice(index=i, message=msg, finish_reason="stop")
454455
choices.append(choice)

0 commit comments

Comments
 (0)