Skip to content

Commit a54f134

Browse files
committed
Stabilize Snapshot Tests by Normalizing Model Outputs
1 parent d05353a commit a54f134

File tree

1 file changed

+30
-12
lines changed
  • tests/model_serving/model_runtime/model_validation

1 file changed

+30
-12
lines changed

tests/model_serving/model_runtime/model_validation/utils.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,45 @@
33
from tests.model_serving.model_runtime.vllm.constant import VLLM_SUPPORTED_QUANTIZATION
44

55

6+
def extract_content_field(output: Any) -> str:
7+
"""
8+
Extract the 'content' field from a typical VLLM chat response.
9+
Returns an empty string if not found.
10+
"""
11+
try:
12+
return output["choices"][0]["message"].get("content", "").strip()
13+
except (KeyError, IndexError, TypeError):
14+
return ""
15+
16+
617
def validate_supported_quantization_schema(q_type: str) -> None:
718
if q_type not in VLLM_SUPPORTED_QUANTIZATION:
819
raise ValueError(f"Unsupported quantization type: {q_type}")
920

1021

11-
def validate_inference_output(*args: tuple[str, ...] | list[Any], response_snapshot: Any) -> None:
12-
for data in args:
13-
assert data == response_snapshot, f"output mismatch for {data}"
14-
22+
def validate_inference_output(response_output: Any, expected_keywords: list[str]) -> None:
23+
"""
24+
Validate inference response output using regex-based keyword checks.
1525
16-
def safe_k8s_name(model_name: str, max_length: int = 20) -> str:
26+
- Extracts 'content' field from model output.
27+
- Fails if content is empty.
28+
- Passes if any of the expected keywords/phrases are found in the content (case-insensitive).
1729
"""
18-
Create a safe Kubernetes name from model_name by truncating to max_length characters
19-
and ensuring it follows Kubernetes naming conventions.
2030

21-
Args:
22-
model_name: The original model name
23-
max_length: Maximum length for the name (default: 20)
31+
content = extract_content_field(response_output)
32+
assert content, "Inference output is empty or missing 'content' field."
33+
found_keywords = [kw for kw in expected_keywords if re.search(rf"\b{re.escape(kw)}\b", content, re.IGNORECASE)]
34+
assert found_keywords, (
35+
f"Expected one of {expected_keywords} in response content, "
36+
f"but got: {content[:900]}..."
37+
)
38+
39+
print(f"Output validation passed. Found keywords: {found_keywords}")
40+
2441

25-
Returns:
26-
A valid Kubernetes name truncated to max_length characters
42+
def safe_k8s_name(model_name: str, max_length: int = 20) -> str:
43+
"""
44+
Generate a Kubernetes-safe model name.
2745
"""
2846
if not model_name:
2947
return "default-model"

0 commit comments

Comments
 (0)