Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f01ca5

Browse files
viniciusdsmellowhoseoyster
authored andcommitted
feat(tracing): enable custom inferenceId for trace decorator
- Add dedicated inference_id field to Trace class instead of storing in metadata - Allow users to set custom inference ID via update_current_trace(inferenceId=...) - Maintain backward compatibility with auto-generated UUID fallback - Simplify post_process_trace logic by removing special metadata filtering - Update examples to demonstrate custom inference ID usage patterns BREAKING CHANGE: None - fully backward compatible Closes: #OPEN-7312
1 parent dfd8184 commit 1f01ca5

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

‎examples/tracing/trace_metadata_updates.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ def handle_user_request(self, request_text: str, session_token: str) -> str:
4141
# Get user session (this info isn't available as function arguments)
4242
user_session = self.get_user_session(session_token)
4343

44-
# Set trace-level metadata with user context
44+
# Set trace-level metadata with user context and custom inference ID
45+
custom_inference_id = f"chat_{user_session.user_id}_{user_session.interaction_count}_{int(datetime.now().timestamp())}"
46+
4547
update_current_trace(
48+
inferenceId=custom_inference_id,
4649
name=f"chat_request_{user_session.user_id}",
4750
user_id=user_session.user_id,
4851
tags=["chat", "user_request", user_session.preferences.get("tier", "free")],
@@ -174,26 +177,29 @@ def make_formal(self, text: str) -> str:
174177
def batch_processing_example():
175178
"""Example showing batch processing with trace metadata updates."""
176179

177-
# Set trace metadata for batch job
180+
# Process multiple requests
181+
test_requests = [
182+
("Hello there!", "premium_session_123"),
183+
("What's the weather like?", "free_session_456"),
184+
("Help me with coding", "premium_session_789")
185+
]
186+
187+
# Set trace metadata for batch job with custom batch ID
188+
batch_inference_id = f"batch_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{len(test_requests)}_items"
189+
178190
update_current_trace(
191+
inferenceId=batch_inference_id,
179192
name="batch_user_requests",
180193
tags=["batch", "processing", "multiple_users"],
181194
metadata={
182-
"batch_size": 3,
195+
"batch_size": len(test_requests),
183196
"processing_start": datetime.now().isoformat(),
184197
}
185198
)
186199

187200
app = ChatApplication()
188201
results = []
189202

190-
# Process multiple requests
191-
test_requests = [
192-
("Hello there!", "premium_session_123"),
193-
("What's the weather like?", "free_session_456"),
194-
("Help me with coding", "premium_session_789")
195-
]
196-
197203
for i, (request, session) in enumerate(test_requests):
198204
result = app.handle_user_request(request, session)
199205
results.append(result)

‎src/openlayer/lib/tracing/tracer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ def update_current_trace(**kwargs) -> None:
779779
>>> def my_function():
780780
>>> # Update trace with user context
781781
>>> update_current_trace(
782+
>>> inferenceId="custom_inference_id",
782783
>>> user_id="user123",
783784
>>> session_id="sess456",
784785
>>> custom_field="any_value"
@@ -1170,7 +1171,7 @@ def post_process_trace(
11701171

11711172
trace_data = {
11721173
"inferenceTimestamp": root_step.start_time,
1173-
"inferenceId": str(root_step.id),
1174+
"inferenceId": trace_obj.inference_id or str(root_step.id),
11741175
"output": root_step.output,
11751176
"latency": root_step.latency,
11761177
"cost": processed_steps[0].get("cost", 0),

‎src/openlayer/lib/tracing/traces.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(self):
1616
self.steps = []
1717
self.current_step = None
1818
self.metadata: Optional[Dict[str, Any]] = None
19+
self.inference_id: Optional[str] = None
1920

2021
def add_step(self, step: Step) -> None:
2122
"""Adds a step to the trace."""
@@ -26,11 +27,16 @@ def update_metadata(self, **kwargs) -> None:
2627
2728
All provided key-value pairs will be stored in self.metadata.
2829
Special handling for 'metadata' key which gets merged with existing metadata.
30+
Special handling for 'inferenceId' which gets stored in dedicated field.
2931
"""
3032
# Initialize metadata if it doesn't exist
3133
if self.metadata is None:
3234
self.metadata = {}
3335

36+
# Handle special case for inferenceId - store in dedicated field
37+
if 'inferenceId' in kwargs:
38+
self.inference_id = kwargs.pop('inferenceId')
39+
3440
# Handle special case for 'metadata' key - merge with existing
3541
if 'metadata' in kwargs:
3642
metadata_to_merge = kwargs.pop('metadata')

0 commit comments

Comments
 (0)