Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(python-sdk): end the original streaming span instead of orphaning it (#1464)

Tai An7bc745992899
diff --git a/sdk/python/src/openlit/instrumentation/ai21/ai21.py b/sdk/python/src/openlit/instrumentation/ai21/ai21.py
index e10fbb48..14cbfc7e 100644
--- a/sdk/python/src/openlit/instrumentation/ai21/ai21.py
+++ b/sdk/python/src/openlit/instrumentation/ai21/ai21.py
@@ -91,9 +91,7 @@ def chat(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py b/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py
index 62319bd8..b493908a 100644
--- a/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py
+++ b/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py
@@ -91,9 +91,7 @@ def async_chat(
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py b/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
index 5bfb5339..de51064d 100644
--- a/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
+++ b/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
@@ -214,9 +214,7 @@ def converse_stream(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py b/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py
index c3aae0f4..448d302c 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py
@@ -174,9 +174,7 @@ def async_chat_stream(
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/cohere/cohere.py b/sdk/python/src/openlit/instrumentation/cohere/cohere.py
index d05b5bba..7ce5f4fa 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/cohere.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/cohere.py
@@ -174,9 +174,7 @@ def chat_stream(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
index 88513a1b..3d0b365e 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
@@ -176,9 +176,7 @@ def async_generate_stream(
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
index a4907055..ce28d0f4 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
@@ -176,9 +176,7 @@ def generate_stream(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py b/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
index d79c1d67..dda1e2f5 100644
--- a/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
@@ -92,9 +92,7 @@ def generate(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_generate_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/groq/async_groq.py b/sdk/python/src/openlit/instrumentation/groq/async_groq.py
index c741a9b1..686f4353 100644
--- a/sdk/python/src/openlit/instrumentation/groq/async_groq.py
+++ b/sdk/python/src/openlit/instrumentation/groq/async_groq.py
@@ -94,9 +94,7 @@ def async_chat(
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/groq/groq.py b/sdk/python/src/openlit/instrumentation/groq/groq.py
index 380e2e24..e9492937 100644
--- a/sdk/python/src/openlit/instrumentation/groq/groq.py
+++ b/sdk/python/src/openlit/instrumentation/groq/groq.py
@@ -94,9 +94,7 @@ def chat(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py b/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py
index 7c65f4ad..38592f29 100644
--- a/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py
+++ b/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py
@@ -171,9 +171,7 @@ def async_stream(
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/mistral/mistral.py b/sdk/python/src/openlit/instrumentation/mistral/mistral.py
index eada7487..dbbbe9bb 100644
--- a/sdk/python/src/openlit/instrumentation/mistral/mistral.py
+++ b/sdk/python/src/openlit/instrumentation/mistral/mistral.py
@@ -171,9 +171,7 @@ def stream(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py b/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py
index 14c4bfde..cb0badb6 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py
@@ -97,9 +97,7 @@ def async_chat(
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -281,9 +279,7 @@ def async_generate(
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_generate_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/ollama/ollama.py b/sdk/python/src/openlit/instrumentation/ollama/ollama.py
index cd5894f4..8ea4e3d5 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/ollama.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/ollama.py
@@ -97,9 +97,7 @@ def chat(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -281,9 +279,7 @@ def generate(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_generate_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/premai/premai.py b/sdk/python/src/openlit/instrumentation/premai/premai.py
index be145e65..0b4ed49e 100644
--- a/sdk/python/src/openlit/instrumentation/premai/premai.py
+++ b/sdk/python/src/openlit/instrumentation/premai/premai.py
@@ -90,9 +90,7 @@ def chat(
finally:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/together/async_together.py b/sdk/python/src/openlit/instrumentation/together/async_together.py
index cdaaea32..9a4d7148 100644
--- a/sdk/python/src/openlit/instrumentation/together/async_together.py
+++ b/sdk/python/src/openlit/instrumentation/together/async_together.py
@@ -89,9 +89,7 @@ def async_completion(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
diff --git a/sdk/python/src/openlit/instrumentation/together/together.py b/sdk/python/src/openlit/instrumentation/together/together.py
index 7ba1a6ba..12d59112 100644
--- a/sdk/python/src/openlit/instrumentation/together/together.py
+++ b/sdk/python/src/openlit/instrumentation/together/together.py
@@ -89,9 +89,7 @@ def completion(
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(
- self._span_name, kind=SpanKind.CLIENT
- ) as self._span:
+ with self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,