From 98189cd20abf1f6af0a3908b69f350c5b30ba077 Mon Sep 17 00:00:00 2001 From: Harsh Kashyap Date: Mon, 6 Jul 2026 11:46:16 +0530 Subject: [PATCH] Fix OpenAI response created timestamp (#16401) ## What this fixes Closes #16400. `get_data_openai()` currently returns `created: null` when callers do not pass a timestamp, and it replaces explicit timestamp values with the current time. This makes non-streaming OpenAI-compatible responses inconsistent with the expected integer `created` timestamp field. ## Change - Preserve explicit `created` values when provided. - Default non-streaming responses to `int(time.time())` when `created` is not provided. - Add focused unit coverage for default timestamps, explicit timestamps, and unchanged streaming chunk shape. ## Verification - `./.venv/bin/python -m pytest test/unit_test/api/utils/test_api_utils.py -q` - `python3 -m py_compile api/utils/api_utils.py test/unit_test/api/utils/test_api_utils.py` - `uvx ruff check api/utils/api_utils.py test/unit_test/api/utils/test_api_utils.py` --------- Co-authored-by: Harsh Kashyap --- api/utils/api_utils.py | 2 +- test/unit_test/api/utils/test_api_utils.py | 48 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/unit_test/api/utils/test_api_utils.py diff --git a/api/utils/api_utils.py b/api/utils/api_utils.py index 973384b8d..97398b3ed 100644 --- a/api/utils/api_utils.py +++ b/api/utils/api_utils.py @@ -444,7 +444,7 @@ def get_data_openai(id=None, created=None, model=None, prompt_tokens=0, completi return { "id": f"{id}", "object": object, - "created": int(time.time()) if created else None, + "created": created if created is not None else int(time.time()), "model": model, "param": param, "usage": { diff --git a/test/unit_test/api/utils/test_api_utils.py b/test/unit_test/api/utils/test_api_utils.py new file mode 100644 index 000000000..08d4d5ade --- /dev/null +++ b/test/unit_test/api/utils/test_api_utils.py @@ -0,0 +1,48 @@ +# +# Copyright 2026 The InfiniFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from api.utils import api_utils + + +def test_get_data_openai_defaults_created_to_current_timestamp(monkeypatch): + monkeypatch.setattr(api_utils.time, "time", lambda: 1234567890.9) + + data = api_utils.get_data_openai(content="answer") + + assert data["created"] == 1234567890 + + +def test_get_data_openai_preserves_explicit_created_value(): + data = api_utils.get_data_openai(created=0, content="answer") + + assert data["created"] == 0 + + +def test_get_data_openai_stream_response_shape_is_unchanged(): + data = api_utils.get_data_openai(id="chatcmpl-test", model="test-model", content="chunk", finish_reason=None, stream=True) + + assert data == { + "id": "chatcmpl-test", + "object": "chat.completion.chunk", + "model": "test-model", + "choices": [ + { + "delta": {"content": "chunk"}, + "finish_reason": None, + "index": 0, + } + ], + }