Skip to content
Draft
Show file tree
Hide file tree
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
67 changes: 63 additions & 4 deletions tests/entrypoints/openai/chat_completion/test_serving_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,7 @@ async def generate_response_from_harmony_str(
req: ChatCompletionRequest,
harmony_str: str,
stream: bool = False,
finish_reason: str | None = None,
) -> ChatCompletionResponse:
harmony_token_ids = get_encoding().encode(harmony_str, allowed_special="all")

Expand All @@ -1294,11 +1295,14 @@ async def result_generator():
req, [token_id]
)
yield self.mock_request_output_from_req_and_token_ids(
req, [], finished=True
req, [], finished=True, finish_reason=finish_reason
)
else:
yield self.mock_request_output_from_req_and_token_ids(
req, harmony_token_ids, finished=True
req,
harmony_token_ids,
finished=True,
finish_reason=finish_reason,
)

generator_func = (
Expand Down Expand Up @@ -1487,8 +1491,8 @@ async def test_harmony_required_tool_choice_render_request_sets_json_schema(
tool_choice="required",
)

generate_request = await (
serving_chat.openai_serving_render.render_chat_request(req)
generate_request = await serving_chat.openai_serving_render.render_chat_request(
req
)

assert not isinstance(generate_request, ErrorResponse)
Expand Down Expand Up @@ -1658,6 +1662,61 @@ async def test_simple_chat(self, serving_chat, stream):
],
)

@pytest.mark.asyncio
async def test_malformed_constraint_recovers_filtered_json(
self, serving_chat, stream
):
req = ChatCompletionRequest(
model=MODEL_NAME,
messages=[{"role": "user", "content": "Return JSON."}],
)
response_str = (
"<|channel|>analysis<|message|>private reasoning<|end|>"
"<|start|>assistant<|channel|>final "
'<|constrain|>response{"ok":true}<|return|>'
)
response = await self.generate_response_from_harmony_str(
serving_chat,
req,
response_str,
stream=stream,
finish_reason="stop",
)

choice = response.choices[0]
assert choice.message.content == '{"ok":true}'
assert choice.finish_reason == "stop"
assert choice.stop_reason is None

@pytest.mark.asyncio
@pytest.mark.parametrize(
"response_str",
[
"<|channel|>analysis<|message|>private reasoning<|return|>",
'<|channel|>final_output<|message|>{"ambiguous":true}<|return|>',
],
ids=["analysis_only", "unknown_channel"],
)
async def test_empty_or_ambiguous_output_is_explicitly_incomplete(
self, serving_chat, stream, response_str
):
req = ChatCompletionRequest(
model=MODEL_NAME,
messages=[{"role": "user", "content": "Return JSON."}],
)
response = await self.generate_response_from_harmony_str(
serving_chat,
req,
response_str,
stream=stream,
finish_reason="stop",
)

choice = response.choices[0]
assert not (choice.message.content or "").strip()
assert choice.finish_reason == "length"
assert choice.stop_reason == "content_null"

@pytest.mark.asyncio
async def test_system_message_without_tools(self, serving_chat, stream):
"""Leading system message produces a developer message with
Expand Down
Loading
Loading