Fix non-conformant UUIDv7 timestamp encoding in the native Opik integration#5
Open
alexkuzmik wants to merge 1 commit into
Open
Fix non-conformant UUIDv7 timestamp encoding in the native Opik integration#5alexkuzmik wants to merge 1 commit into
alexkuzmik wants to merge 1 commit into
Conversation
create_uuid7() encoded the timestamp in units of 16 seconds instead of milliseconds, so the top 48 bits came out ~4096x the real unix-ms. Opik's backend validates the embedded UUIDv7 timestamp on ingestion (OPIK-7067); the bad encoding decoded to ~year 2201 and every trace/span batch was rejected with HTTP 400. Rewrite create_uuid7() to be RFC 9562 conformant (top 48 bits = unix-ms), using the standard library only so no new dependency is added. Add unit tests covering UUIDv7 validity and millisecond timestamp encoding. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title
Fix non-conformant UUIDv7 timestamp encoding in the native Opik integration
Relevant issues
OPIK-7067
Pre-Submission checklist
tests/test_litellm/directory —tests/test_litellm/integrations/test_opik_utils.py(2 tests)make test-unit(tests/test_litellm)Type
🐛 Bug Fix
Changes
The bug: the timestamp encoded into the UUID was wrong
The native Opik callback (the
opikcallback bundled inlitellm, notopik.integrations.litellm) mints trace/span ids withcreate_uuid7()inlitellm/integrations/opik/utils.py.A UUIDv7 must encode, in its top 48 bits, the Unix timestamp in milliseconds (RFC 9562). The old implementation instead encoded time in units of 16 seconds:
This made the embedded timestamp come out at roughly 4096× the real unix-ms. Decoding the top 48 bits of a generated id (the way a consumer reads a UUIDv7 timestamp) yields a date ~175 years in the future (e.g. year 2201) instead of "now".
This matters because Opik's backend validates the timestamp embedded in incoming UUIDv7 ids and rejects ids whose timestamp is implausibly far from the present — so every trace/span batch from the native integration was failing ingestion with HTTP 400.
The fix
Rewrite
create_uuid7()to be RFC 9562 conformant: the top 48 bits now hold the real Unix-millisecond timestamp, followed by the version (7) and variant (0b10) bits and random data for the remainder.time,os.urandom, anduuid; no new dependency is added to litellm. (opik.id_helperswas used only as a reference.)Tests
tests/test_litellm/integrations/test_opik_utils.py:test_create_uuid7_is_valid_version_7_uuid— asserts the result is a valid UUID with version 7 and the RFC 4122 variant.test_create_uuid7_encodes_timestamp_in_milliseconds— pins the clock to a fixed instant and asserts the top 48 bits decode to that instant in milliseconds (the exact behavior the old code got wrong).