test: generate unique chat completion IDs for replayed responses

When replaying recorded chat completion responses, the original chat IDs
cause conflicts due to SQLite unique constraints. Generate new UUIDs for
both ChatCompletion and ChatCompletionChunk objects to ensure each
replayed response has a unique identifier.

This fixes test failures when running integration tests in replay mode
with recorded chat completion responses.
This commit is contained in:
Derek Higgins 2025-08-13 14:05:16 +01:00
parent 6358d0a478
commit 711735891a

View file

@ -10,12 +10,15 @@ import hashlib
import json
import os
import sqlite3
import uuid
from collections.abc import Generator
from contextlib import contextmanager
from enum import StrEnum
from pathlib import Path
from typing import Any, Literal, cast
from openai.types.chat import ChatCompletion, ChatCompletionChunk
from llama_stack.log import get_logger
logger = get_logger(__name__, category="testing")
@ -248,6 +251,20 @@ async def _patched_inference_method(original_method, self, client_type, endpoint
recording = _current_storage.find_recording(request_hash)
if recording:
response_body = recording["response"]["body"]
if (
isinstance(response_body, list)
and len(response_body) > 0
and isinstance(response_body[0], ChatCompletionChunk)
):
# We can't replay chatcompletions with the same id and we store them in a sqlite database with a unique constraint on the id.
# So we generate a new id and replace the old one.
newid = uuid.uuid4().hex
response_body[0].id = "chatcmpl-" + newid
elif isinstance(response_body, ChatCompletion):
# We can't replay chatcompletions with the same id and we store them in a sqlite database with a unique constraint on the id.
# So we generate a new id and replace the old one.
newid = uuid.uuid4().hex
response_body.id = "chatcmpl-" + newid
if recording["response"].get("is_streaming", False):