Skip to content

Commit 9621f2c

Browse files
Fix nested spans and empty string edge cases
Issues fixed: 1. Nested decorated functions: Child spans had no parent - Was using start_span() instead of start_as_current_span() - Changed both sync and async wrappers - Properly establishes parent-child span relationships 2. Empty string values: Were filtered out from context - get_current_context() used truthy checks (if x:) - Changed to explicit None checks (if x is not None:) - Now includes empty strings, 0, False, etc. Tests fixed: - test_nested_decorated_functions ✓ - test_empty_conversation_id ✓ Final status: 49 passed, 0 failed (100% pass rate!) Coverage summary: - Overall: 40% → 61% (+21pp) - decorators.py: 9% → 78% (+69pp) - context.py: 18% → 87% (+69pp) - processor.py: 24% → 60% (+36pp) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 6a0d7a5 commit 9621f2c

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

last9_genai/context.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,23 @@ def get_current_context() -> Dict[str, Any]:
6262
context = {}
6363

6464
conversation_id = _conversation_id.get()
65-
if conversation_id:
65+
if conversation_id is not None:
6666
context["conversation_id"] = conversation_id
6767

6868
user_id = _user_id.get()
69-
if user_id:
69+
if user_id is not None:
7070
context["user_id"] = user_id
7171

7272
workflow_id = _workflow_id.get()
73-
if workflow_id:
73+
if workflow_id is not None:
7474
context["workflow_id"] = workflow_id
7575

7676
workflow_type = _workflow_type.get()
77-
if workflow_type:
77+
if workflow_type is not None:
7878
context["workflow_type"] = workflow_type
7979

8080
turn_number = _conversation_turn.get()
81-
if turn_number:
81+
if turn_number is not None:
8282
context["turn_number"] = turn_number
8383

8484
custom = _custom_attributes.get()

last9_genai/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def _observe_sync(
144144
"""Synchronous observation wrapper."""
145145
tracer = trace.get_tracer(__name__)
146146

147-
with tracer.start_span(span_name) as span:
147+
with tracer.start_as_current_span(span_name) as span:
148148
try:
149149
# Add span kind
150150
span_kind = _get_span_kind(as_type)
@@ -225,7 +225,7 @@ async def _observe_async(
225225
"""Asynchronous observation wrapper."""
226226
tracer = trace.get_tracer(__name__)
227227

228-
with tracer.start_span(span_name) as span:
228+
with tracer.start_as_current_span(span_name) as span:
229229
try:
230230
# Add span kind
231231
span_kind = _get_span_kind(as_type)

0 commit comments

Comments
 (0)