From 7ed3c5ecc9f457ed041b0de5eddac87449559280 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Mon, 2 Nov 2026 21:36:49 -0500 Subject: [PATCH] fix(chat): return a Unix timestamp in the OpenAI-compatible 'created' field created was set to timezone.now().second, i.e. the seconds field of the clock (0-59), instead of the Unix timestamp the OpenAI API specifies. Co-Authored-By: Claude Sonnet 5.5 --- .../impl/response/openai_to_response.py | 4 ++-- apps/common/tests_openai_to_response.py | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 apps/common/tests_openai_to_response.py diff --git a/apps/common/handle/impl/response/openai_to_response.py b/apps/common/handle/impl/response/openai_to_response.py index b4eda362555..123ebc2efaf 100644 --- a/apps/common/handle/impl/response/openai_to_response.py +++ b/apps/common/handle/impl/response/openai_to_response.py @@ -29,7 +29,7 @@ def to_block_response(self, chat_id, chat_record_id, content, is_end, prompt_tok BlockChoice(finish_reason='stop', index=0, chat_id=chat_id, answer_list=other_params.get('answer_list', ""), message=ChatCompletionMessage(role='assistant', content=content))], - created=timezone.now().second, model='', object='chat.completion', + created=int(timezone.now().timestamp()), model='', object='chat.completion', usage=CompletionUsage(completion_tokens=completion_tokens, prompt_tokens=prompt_tokens, total_tokens=completion_tokens + prompt_tokens) @@ -42,7 +42,7 @@ def to_stream_chunk_response(self, chat_id, chat_record_id, node_id, up_node_id_ if other_params is None: other_params = {} chunk = ChatCompletionChunk(id=chat_record_id, model='', object='chat.completion.chunk', - created=timezone.now().second, choices=[ + created=int(timezone.now().timestamp()), choices=[ Choice(delta=ChoiceDelta(content=content, reasoning_content=other_params.get('reasoning_content', ""), chat_id=chat_id), finish_reason='stop' if is_end else None, diff --git a/apps/common/tests_openai_to_response.py b/apps/common/tests_openai_to_response.py new file mode 100644 index 00000000000..80f29b41c67 --- /dev/null +++ b/apps/common/tests_openai_to_response.py @@ -0,0 +1,23 @@ +import datetime +import json +import unittest +from unittest import mock + +from common.handle.impl.response.openai_to_response import OpenaiToResponse + + +class OpenaiToResponseCreatedTestCase(unittest.TestCase): + """`created` must be a Unix timestamp, as in the OpenAI API, not the seconds field of the clock.""" + + NOW = datetime.datetime(2026, 11, 3, 2, 35, 7, tzinfo=datetime.timezone.utc) + + def test_block_response_created_is_unix_timestamp(self): + with mock.patch('common.handle.impl.response.openai_to_response.timezone.now', return_value=self.NOW): + response = OpenaiToResponse().to_block_response('chat', 'record', 'hi', True, 1, 2) + self.assertEqual(json.loads(response.content)['created'], int(self.NOW.timestamp())) + + def test_stream_chunk_created_is_unix_timestamp(self): + with mock.patch('common.handle.impl.response.openai_to_response.timezone.now', return_value=self.NOW): + chunk = OpenaiToResponse().to_stream_chunk_response('chat', 'record', 'node', [], 'hi', False, 1, 2) + payload = json.loads(chunk[len('data: '):]) + self.assertEqual(payload['created'], int(self.NOW.timestamp()))