Skip to content

Commit 657a1fa

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 7ca8233 commit 657a1fa

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
@@ -9,12 +9,15 @@
99
import hashlib
1010
import json
1111
import os
12+
import uuid
1213
from collections.abc import Generator
1314
from contextlib import contextmanager
1415
from enum import StrEnum
1516
from pathlib import Path
1617
from typing import Any, Literal, cast
1718

19+
from openai.types.chat import ChatCompletion, ChatCompletionChunk
20+
1821
from llama_stack.log import get_logger
1922

2023
logger = get_logger(__name__, category="testing")
@@ -207,6 +210,20 @@ async def _patched_inference_method(original_method, self, client_type, endpoint
207210
recording = _current_storage.find_recording(request_hash)
208211
if recording:
209212
response_body = recording["response"]["body"]
213+
if (
214+
isinstance(response_body, list)
215+
and len(response_body) > 0
216+
and isinstance(response_body[0], ChatCompletionChunk)
217+
):
218+
# We can't replay chatcompletions with the same id and we store them in a sqlite database with a unique constraint on the id.
219+
# So we generate a new id and replace the old one.
220+
newid = uuid.uuid4().hex
221+
response_body[0].id = "chatcmpl-" + newid
222+
elif isinstance(response_body, ChatCompletion):
223+
# We can't replay chatcompletions with the same id and we store them in a sqlite database with a unique constraint on the id.
224+
# So we generate a new id and replace the old one.
225+
newid = uuid.uuid4().hex
226+
response_body.id = "chatcmpl-" + newid
210227

211228
if recording["response"].get("is_streaming", False):
212229

0 commit comments

Comments
 (0)