Skip to content

Commit 3ae4ad8

Browse files
committed
Switch to using a seed based on run_id
1 parent 6185d25 commit 3ae4ad8

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

temporalio/contrib/openai_agents/_temporal_trace_provider.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""Provides support for integration with OpenAI Agents SDK tracing across workflows"""
22

33
import uuid
4-
from random import Random
54
from types import TracebackType
65
from typing import Any, Optional, cast
7-
from uuid import UUID
86

97
from agents import SpanData, Trace, TracingProcessor
108
from agents.tracing import (
@@ -17,6 +15,7 @@
1715
from agents.tracing.spans import Span
1816

1917
from temporalio import workflow
18+
from temporalio.contrib.openai_agents._trace_interceptor import RunIdRandom
2019
from temporalio.workflow import ReadOnlyContextError
2120

2221

@@ -137,11 +136,9 @@ def force_flush(self) -> None:
137136

138137
def _workflow_uuid() -> str:
139138
random = cast(
140-
Random, getattr(workflow.instance(), "__temporal_openai_tracing_random")
139+
RunIdRandom, getattr(workflow.instance(), "__temporal_openai_tracing_random")
141140
)
142-
return UUID(bytes=random.getrandbits(16 * 8).to_bytes(16, "big"), version=4).hex[
143-
:24
144-
]
141+
return random.uuid4()
145142

146143

147144
class TemporalTraceProvider(DefaultTraceProvider):

temporalio/contrib/openai_agents/_trace_interceptor.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
from __future__ import annotations
44

55
import random
6+
import uuid
67
from contextlib import contextmanager
7-
from typing import Any, Mapping, Protocol, Type, cast
8+
from typing import Any, Mapping, Protocol, Type
89

910
from agents import CustomSpanData, custom_span, get_current_span, trace
1011
from agents.tracing import (
1112
get_trace_provider,
1213
)
13-
from agents.tracing.provider import DefaultTraceProvider
1414
from agents.tracing.scope import Scope
15-
from agents.tracing.spans import NoOpSpan, SpanImpl
15+
from agents.tracing.spans import NoOpSpan
1616

1717
import temporalio.activity
1818
import temporalio.api.common.v1
@@ -284,15 +284,29 @@ async def execute_activity(
284284
return await self.next.execute_activity(input)
285285

286286

287+
class RunIdRandom:
288+
"""Random uuid generator seeded by the run id of the workflow.
289+
Doesn't currently support replay over reset correctly.
290+
"""
291+
292+
def __init__(self):
293+
"""Create a new random UUID generator."""
294+
self._random = random.Random("OpenAIPlugin" + workflow.info().run_id)
295+
296+
def uuid4(self) -> str:
297+
"""Generate a random UUID."""
298+
return uuid.UUID(
299+
bytes=random.getrandbits(16 * 8).to_bytes(16, "big"), version=4
300+
).hex[:24]
301+
302+
287303
def _ensure_tracing_random() -> None:
288304
instance = workflow.instance()
289305
if not hasattr(instance, "__temporal_openai_tracing_random"):
290-
new_random = random.Random()
291-
new_random.setstate(workflow.random().getstate())
292306
setattr(
293307
workflow.instance(),
294308
"__temporal_openai_tracing_random",
295-
new_random,
309+
RunIdRandom(),
296310
)
297311

298312

@@ -376,6 +390,7 @@ def start_activity(
376390
span = custom_span(
377391
name="temporal:startActivity", data={"activity": input.activity}
378392
)
393+
span.finish()
379394
span.start(mark_as_current=True)
380395
set_header_from_context(input, temporalio.workflow.payload_converter())
381396
handle = self.next.start_activity(input)

0 commit comments

Comments
 (0)