Skip to content

Commit 06566d0

Browse files
committed
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.
1 parent 5e7c225 commit 06566d0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

llama_stack/testing/inference_recorder.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
import json
1111
import os
1212
import sqlite3
13+
import uuid
1314
from collections.abc import Generator
1415
from contextlib import contextmanager
1516
from enum import StrEnum
1617
from pathlib import Path
1718
from typing import Any, Literal, cast
1819

20+
from openai.types.chat import ChatCompletion, ChatCompletionChunk
21+
1922
from llama_stack.log import get_logger
2023

2124
logger = get_logger(__name__, category="testing")
@@ -248,6 +251,20 @@ async def _patched_inference_method(original_method, self, client_type, endpoint
248251
recording = _current_storage.find_recording(request_hash)
249252
if recording:
250253
response_body = recording["response"]["body"]
254+
if (
255+
isinstance(response_body, list)
256+
and len(response_body) > 0
257+
and isinstance(response_body[0], ChatCompletionChunk)
258+
):
259+
# We can't replay chatcompletions with the same id and we store them in a sqlite database with a unique constraint on the id.
260+
# So we generate a new id and replace the old one.
261+
newid = uuid.uuid4().hex
262+
response_body[0].id = "chatcmpl-" + newid
263+
elif isinstance(response_body, ChatCompletion):
264+
# We can't replay chatcompletions with the same id and we store them in a sqlite database with a unique constraint on the id.
265+
# So we generate a new id and replace the old one.
266+
newid = uuid.uuid4().hex
267+
response_body.id = "chatcmpl-" + newid
251268

252269
if recording["response"].get("is_streaming", False):
253270

0 commit comments

Comments
 (0)