Skip to content

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

feat(python-sdk): capture reasoning content for Groq and Ollama chat (#1348)

Aditya Jethani56dfeb472b56
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index 1cfbcb15..09c74d5b 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -198,6 +198,45 @@ def truncate_content(text):
return s
+def extract_reasoning_content(payload, *field_names):
+ """Return the first non-empty string reasoning field from *payload*.
+
+ Defaults to OpenAI-compatible keys ``reasoning_content`` then ``reasoning``.
+ Pass alternate keys (e.g. ``\"thinking\"``) for providers like Ollama.
+ """
+ if not isinstance(payload, dict):
+ return ""
+ keys = field_names or ("reasoning_content", "reasoning")
+ for key in keys:
+ value = payload.get(key)
+ if isinstance(value, str) and value:
+ return value
+ return ""
+
+
+def append_scope_reasoning(scope, text):
+ """Append reasoning/thinking text onto ``scope._reasoning_content``."""
+ if not text or not isinstance(text, str):
+ return
+ current = getattr(scope, "_reasoning_content", None)
+ if not current:
+ scope._reasoning_content = text
+ else:
+ scope._reasoning_content = current + text
+
+
+def set_span_reasoning_content(span, scope, capture_message_content=True):
+ """Set ``gen_ai.content.reasoning`` when content capture is enabled."""
+ if not capture_message_content or span is None:
+ return
+ reasoning = getattr(scope, "_reasoning_content", None)
+ if reasoning:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_REASONING,
+ truncate_content(reasoning),
+ )
+
+
def truncate_message_content(messages):
"""Apply truncation to text content fields within OTel message structures.
diff --git a/sdk/python/src/openlit/instrumentation/groq/utils.py b/sdk/python/src/openlit/instrumentation/groq/utils.py
index 1d927124..93c90060 100644
--- a/sdk/python/src/openlit/instrumentation/groq/utils.py
+++ b/sdk/python/src/openlit/instrumentation/groq/utils.py
@@ -9,10 +9,12 @@ import time
from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
+ append_scope_reasoning,
apply_agent_version_attributes,
build_system_instructions_from_messages,
build_tool_definitions,
calculate_ttft,
+ extract_reasoning_content,
response_as_dict,
calculate_tbt,
general_tokens,
@@ -20,6 +22,7 @@ from openlit.__helpers import (
common_span_attributes,
record_completion_metrics,
otel_event,
+ set_span_reasoning_content,
truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -272,15 +275,15 @@ def process_chunk(scope, chunk):
chunked = response_as_dict(chunk)
- # Collect message IDs and aggregated response from events
- if (
- len(chunked.get("choices", [])) > 0
- and "delta" in chunked.get("choices")[0]
- and "content" in chunked.get("choices")[0].get("delta", {})
- ):
- content = chunked.get("choices")[0].get("delta").get("content")
+ # Collect content + reasoning from streaming deltas (Groq reasoning models
+ # emit chain-of-thought on delta.reasoning when reasoning_format=parsed).
+ choices = chunked.get("choices") or []
+ if choices and isinstance(choices[0], dict) and "delta" in choices[0]:
+ delta = choices[0].get("delta") or {}
+ content = delta.get("content")
if content:
scope._llmresponse += content
+ append_scope_reasoning(scope, extract_reasoning_content(delta, "reasoning"))
if chunked.get("x_groq") is not None:
if chunked.get("x_groq").get("usage") is not None:
@@ -474,6 +477,7 @@ def common_chat_logic(
# Span Attributes for Content
if capture_message_content:
_set_span_messages_as_array(scope._span, input_msgs, output_msgs)
+ set_span_reasoning_content(scope._span, scope, capture_message_content=True)
if system_instr:
scope._span.set_attribute(
SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS,
@@ -617,6 +621,16 @@ def process_chat_response(
(choice.get("message", {}).get("content") or "")
for choice in response_dict.get("choices", [])
)
+ # Handle reasoning content from a non-streaming response (Groq reasoning
+ # models). Aggregate across all choices to stay consistent with how
+ # scope._llmresponse concatenates content when n > 1.
+ reasoning_parts = [
+ extract_reasoning_content(choice.get("message", {}), "reasoning")
+ for choice in response_dict.get("choices", [])
+ ]
+ reasoning = " ".join(part for part in reasoning_parts if part).strip()
+ if reasoning:
+ scope._reasoning_content = reasoning
scope._response_id = response_dict.get("id")
scope._response_model = response_dict.get("model") or request_model
# Handle token usage including reasoning tokens and cached tokens
diff --git a/sdk/python/src/openlit/instrumentation/ollama/utils.py b/sdk/python/src/openlit/instrumentation/ollama/utils.py
index 150d65df..9d65a0ed 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/utils.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/utils.py
@@ -9,9 +9,11 @@ import time
from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
+ append_scope_reasoning,
apply_agent_version_attributes,
build_system_instructions_from_messages,
calculate_ttft,
+ extract_reasoning_content,
response_as_dict,
calculate_tbt,
general_tokens,
@@ -21,6 +23,7 @@ from openlit.__helpers import (
record_completion_metrics,
record_embedding_metrics,
otel_event,
+ set_span_reasoning_content,
truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -274,13 +277,24 @@ def process_chunk(self, chunk):
self._ttft = calculate_ttft(self._timestamps, self._start_time)
chunked = response_as_dict(chunk)
- self._llmresponse += chunked.get("message", {}).get("content", "")
- if chunked.get("message", {}).get("tool_calls"):
- self._tools = chunked["message"]["tool_calls"]
+ # Chat streaming uses message.content / message.thinking.
+ # Generate streaming uses top-level response / thinking.
+ message = chunked.get("message") or {}
+ content = message.get("content") or chunked.get("response") or ""
+ if content:
+ self._llmresponse += content
+
+ thinking = extract_reasoning_content(message, "thinking") or extract_reasoning_content(
+ chunked, "thinking"
+ )
+ append_scope_reasoning(self, thinking)
+
+ if message.get("tool_calls"):
+ self._tools = message["tool_calls"]
if chunked.get("eval_count"):
- self._response_role = chunked.get("message", {}).get("role", "")
+ self._response_role = message.get("role", "")
# Handle token usage including reasoning tokens and cached tokens
self._input_tokens = chunked.get("prompt_eval_count", 0)
self._output_tokens = chunked.get("eval_count", 0)
@@ -429,6 +443,7 @@ def common_chat_logic(
if capture_message_content:
_set_span_messages_as_array(scope._span, input_msgs, output_msgs)
+ set_span_reasoning_content(scope._span, scope, capture_message_content=True)
if system_instr:
scope._span.set_attribute(
SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS,
@@ -628,6 +643,7 @@ def common_generate_logic(
scope._llmresponse, scope._finish_reason, scope._tools
)
_set_span_messages_as_array(scope._span, input_msgs, output_msgs)
+ set_span_reasoning_content(scope._span, scope, capture_message_content=True)
# Emit inference event
if event_provider:
@@ -864,6 +880,12 @@ def process_chat_response(
scope._span = span
scope._llmresponse = response_dict.get("message", {}).get("content", "")
scope._response_role = response_dict.get("message", {}).get("role", "assistant")
+ # Handle reasoning content from a non-streaming chat response (think=True)
+ thinking = extract_reasoning_content(
+ response_dict.get("message") or {}, "thinking"
+ )
+ if thinking:
+ scope._reasoning_content = thinking
# Handle token usage including reasoning tokens and cached tokens
scope._input_tokens = response_dict.get("prompt_eval_count", 0)
scope._output_tokens = response_dict.get("eval_count", 0)
@@ -955,6 +977,10 @@ def process_generate_response(
scope._span = span
scope._llmresponse = response_dict.get("response", "")
scope._response_role = response_dict.get("message", {}).get("role", "assistant")
+ # Generate responses put thinking at the top level when think=True
+ thinking = extract_reasoning_content(response_dict, "thinking")
+ if thinking:
+ scope._reasoning_content = thinking
# Handle token usage including reasoning tokens and cached tokens
scope._input_tokens = response_dict.get("prompt_eval_count", 0)
scope._output_tokens = response_dict.get("eval_count", 0)
diff --git a/sdk/python/tests/test_reasoning_content.py b/sdk/python/tests/test_reasoning_content.py
new file mode 100644
index 00000000..b07bc2b4
--- /dev/null
+++ b/sdk/python/tests/test_reasoning_content.py
@@ -0,0 +1,389 @@
+# pylint: disable=protected-access
+"""
+Unit tests for reasoning-content capture (``gen_ai.content.reasoning``) in the
+Groq and Ollama instrumentations.
+
+These tests drive the instrumentation helpers directly with mocked provider
+responses, so they need neither a live API key nor a running model.
+"""
+
+import time
+from types import SimpleNamespace
+
+import pytest
+from opentelemetry.sdk.trace import TracerProvider
+from opentelemetry.sdk.trace.export import SimpleSpanProcessor
+from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
+ InMemorySpanExporter,
+)
+
+from openlit._config import OpenlitConfig
+from openlit.__helpers import (
+ append_scope_reasoning,
+ extract_reasoning_content,
+ set_span_reasoning_content,
+)
+from openlit.instrumentation.groq import utils as groq_utils
+from openlit.instrumentation.ollama import utils as ollama_utils
+from openlit.semcov import SemanticConvention
+
+REASONING_ATTR = SemanticConvention.GEN_AI_CONTENT_REASONING
+
+
[email protected](autouse=True)
+def _reset_openlit_config():
+ """The span helpers read global OpenlitConfig; initialize it to defaults."""
+ OpenlitConfig.reset_to_defaults()
+
+
+def _tracer_with_exporter():
+ """Return (tracer, exporter) backed by an in-memory span exporter."""
+ exporter = InMemorySpanExporter()
+ provider = TracerProvider()
+ provider.add_span_processor(SimpleSpanProcessor(exporter))
+ tracer = provider.get_tracer(__name__)
+ return tracer, exporter
+
+
+def test_extract_reasoning_content_defaults_and_custom_keys():
+ """Shared helper prefers OpenAI keys by default and accepts custom keys."""
+ assert extract_reasoning_content({"reasoning": "a"}) == "a"
+ assert extract_reasoning_content({"reasoning_content": "b", "reasoning": "a"}) == "b"
+ assert extract_reasoning_content({"thinking": "c"}, "thinking") == "c"
+ assert extract_reasoning_content({"thinking": 123}, "thinking") == ""
+ assert extract_reasoning_content("not-a-dict") == ""
+
+
+def test_groq_non_streaming_captures_reasoning():
+ """Groq non-streaming: reasoning maps to gen_ai.content.reasoning on the span."""
+ tracer, exporter = _tracer_with_exporter()
+
+ response = {
+ "id": "resp-1",
+ "model": "qwen/qwen3-32b",
+ "choices": [
+ {
+ "finish_reason": "stop",
+ "message": {
+ "role": "assistant",
+ "content": "The answer is 4.",
+ "reasoning": "2 + 2 = 4, so the answer is 4.",
+ },
+ }
+ ],
+ "usage": {"prompt_tokens": 10, "completion_tokens": 5},
+ }
+
+ with tracer.start_as_current_span("groq.chat") as span:
+ groq_utils.process_chat_response(
+ response=response,
+ request_model="qwen/qwen3-32b",
+ pricing_info={},
+ server_port=443,
+ server_address="api.groq.com",
+ environment="test",
+ application_name="test",
+ metrics={},
+ start_time=time.time(),
+ span=span,
+ capture_message_content=True,
+ disable_metrics=True,
+ version="1.0.0",
+ event_provider=None,
+ messages=[{"role": "user", "content": "what is 2 + 2?"}],
+ model="qwen/qwen3-32b",
+ )
+
+ finished = exporter.get_finished_spans()
+ assert len(finished) == 1
+ assert finished[0].attributes.get(REASONING_ATTR) == "2 + 2 = 4, so the answer is 4."
+
+
+def test_groq_non_streaming_without_reasoning_omits_attribute():
+ """Groq non-streaming: no reasoning field means the attribute is not set."""
+ tracer, exporter = _tracer_with_exporter()
+
+ response = {
+ "id": "resp-2",
+ "model": "llama-3.3-70b-versatile",
+ "choices": [
+ {"finish_reason": "stop", "message": {"role": "assistant", "content": "Hi!"}}
+ ],
+ "usage": {"prompt_tokens": 3, "completion_tokens": 1},
+ }
+
+ with tracer.start_as_current_span("groq.chat") as span:
+ groq_utils.process_chat_response(
+ response=response,
+ request_model="llama-3.3-70b-versatile",
+ pricing_info={},
+ server_port=443,
+ server_address="api.groq.com",
+ environment="test",
+ application_name="test",
+ metrics={},
+ start_time=time.time(),
+ span=span,
+ capture_message_content=True,
+ disable_metrics=True,
+ version="1.0.0",
+ event_provider=None,
+ messages=[{"role": "user", "content": "hi"}],
+ model="llama-3.3-70b-versatile",
+ )
+
+ finished = exporter.get_finished_spans()
+ assert len(finished) == 1
+ assert REASONING_ATTR not in (finished[0].attributes or {})
+
+
+def test_groq_non_streaming_aggregates_reasoning_across_choices():
+ """Groq non-streaming with n > 1: reasoning is aggregated across all choices."""
+ tracer, exporter = _tracer_with_exporter()
+
+ response = {
+ "id": "resp-3",
+ "model": "qwen/qwen3-32b",
+ "choices": [
+ {
+ "finish_reason": "stop",
+ "message": {"role": "assistant", "content": "A", "reasoning": "first."},
+ },
+ {
+ "finish_reason": "stop",
+ "message": {"role": "assistant", "content": "B", "reasoning": "second."},
+ },
+ ],
+ "usage": {"prompt_tokens": 5, "completion_tokens": 2},
+ }
+
+ with tracer.start_as_current_span("groq.chat") as span:
+ groq_utils.process_chat_response(
+ response=response,
+ request_model="qwen/qwen3-32b",
+ pricing_info={},
+ server_port=443,
+ server_address="api.groq.com",
+ environment="test",
+ application_name="test",
+ metrics={},
+ start_time=time.time(),
+ span=span,
+ capture_message_content=True,
+ disable_metrics=True,
+ version="1.0.0",
+ event_provider=None,
+ messages=[{"role": "user", "content": "hi"}],
+ model="qwen/qwen3-32b",
+ )
+
+ finished = exporter.get_finished_spans()
+ assert len(finished) == 1
+ assert finished[0].attributes.get(REASONING_ATTR) == "first. second."
+
+
+def test_groq_omits_reasoning_when_content_capture_disabled():
+ """capture_message_content=False must not emit gen_ai.content.reasoning."""
+ tracer, exporter = _tracer_with_exporter()
+
+ response = {
+ "id": "resp-4",
+ "model": "qwen/qwen3-32b",
+ "choices": [
+ {
+ "finish_reason": "stop",
+ "message": {
+ "role": "assistant",
+ "content": "4",
+ "reasoning": "secret thinking",
+ },
+ }
+ ],
+ "usage": {"prompt_tokens": 2, "completion_tokens": 1},
+ }
+
+ with tracer.start_as_current_span("groq.chat") as span:
+ groq_utils.process_chat_response(
+ response=response,
+ request_model="qwen/qwen3-32b",
+ pricing_info={},
+ server_port=443,
+ server_address="api.groq.com",
+ environment="test",
+ application_name="test",
+ metrics={},
+ start_time=time.time(),
+ span=span,
+ capture_message_content=False,
+ disable_metrics=True,
+ version="1.0.0",
+ event_provider=None,
+ messages=[{"role": "user", "content": "2+2"}],
+ model="qwen/qwen3-32b",
+ )
+
+ finished = exporter.get_finished_spans()
+ assert len(finished) == 1
+ assert REASONING_ATTR not in (finished[0].attributes or {})
+
+
+def test_groq_streaming_accumulates_reasoning():
+ """Groq streaming: delta.reasoning chunks accumulate into the reasoning text."""
+ scope = SimpleNamespace(
+ _timestamps=[],
+ _start_time=time.time(),
+ _ttft=0,
+ _llmresponse="",
+ _tools=None,
+ )
+
+ for delta_reasoning in ("Let me ", "think it ", "through."):
+ groq_utils.process_chunk(
+ scope,
+ {
+ "choices": [
+ {
+ "delta": {"content": "", "reasoning": delta_reasoning},
+ "finish_reason": None,
+ }
+ ]
+ },
+ )
+
+ assert scope._reasoning_content == "Let me think it through."
+
+
+def test_ollama_non_streaming_captures_reasoning():
+ """Ollama non-streaming chat: message.thinking maps to gen_ai.content.reasoning."""
+ tracer, exporter = _tracer_with_exporter()
+
+ response = {
+ "model": "qwen3",
+ "done_reason": "stop",
+ "message": {
+ "role": "assistant",
+ "content": "The answer is 4.",
+ "thinking": "I need to add 2 and 2, which gives 4.",
+ },
+ "prompt_eval_count": 10,
+ "eval_count": 5,
+ }
+
+ with tracer.start_as_current_span("ollama.chat") as span:
+ ollama_utils.process_chat_response(
+ response=response,
+ pricing_info={},
+ server_port=11434,
+ server_address="127.0.0.1",
+ environment="test",
+ application_name="test",
+ metrics={},
+ start_time=time.monotonic(),
+ span=span,
+ capture_message_content=True,
+ disable_metrics=True,
+ version="1.0.0",
+ event_provider=None,
+ messages=[{"role": "user", "content": "what is 2 + 2?"}],
+ model="qwen3",
+ )
+
+ finished = exporter.get_finished_spans()
+ assert len(finished) == 1
+ assert (
+ finished[0].attributes.get(REASONING_ATTR)
+ == "I need to add 2 and 2, which gives 4."
+ )
+
+
+def test_ollama_streaming_chat_accumulates_reasoning():
+ """Ollama chat streaming: message.thinking chunks accumulate."""
+ scope = SimpleNamespace(
+ _timestamps=[],
+ _start_time=time.monotonic(),
+ _ttft=0,
+ _llmresponse="",
+ _tools=None,
+ )
+
+ for thinking in ("Adding ", "two and two ", "makes four."):
+ ollama_utils.process_chunk(
+ scope,
+ {"message": {"content": "", "thinking": thinking}},
+ )
+
+ assert scope._reasoning_content == "Adding two and two makes four."
+
+
+def test_ollama_streaming_generate_accumulates_thinking_and_response():
+ """Ollama generate streaming uses top-level thinking + response fields."""
+ scope = SimpleNamespace(
+ _timestamps=[],
+ _start_time=time.monotonic(),
+ _ttft=0,
+ _llmresponse="",
+ _tools=None,
+ )
+
+ ollama_utils.process_chunk(
+ scope, {"response": "Hi", "thinking": "Warming up. "}
+ )
+ ollama_utils.process_chunk(
+ scope, {"response": " there", "thinking": "Done."}
+ )
+
+ assert scope._llmresponse == "Hi there"
+ assert scope._reasoning_content == "Warming up. Done."
+
+
+def test_ollama_generate_non_streaming_captures_thinking():
+ """Ollama generate non-streaming: top-level thinking on the span."""
+ tracer, exporter = _tracer_with_exporter()
+
+ response = {
+ "model": "qwen3",
+ "done_reason": "stop",
+ "response": "The answer is 4.",
+ "thinking": "Add 2 and 2.",
+ "prompt_eval_count": 8,
+ "eval_count": 4,
+ }
+
+ with tracer.start_as_current_span("ollama.generate") as span:
+ ollama_utils.process_generate_response(
+ response=response,
+ pricing_info={},
+ server_port=11434,
+ server_address="127.0.0.1",
+ environment="test",
+ application_name="test",
+ metrics={},
+ start_time=time.monotonic(),
+ span=span,
+ capture_message_content=True,
+ disable_metrics=True,
+ version="1.0.0",
+ event_provider=None,
+ model="qwen3",
+ prompt="what is 2 + 2?",
+ json={"model": "qwen3", "prompt": "what is 2 + 2?"},
+ )
+
+ finished = exporter.get_finished_spans()
+ assert len(finished) == 1
+ assert finished[0].attributes.get(REASONING_ATTR) == "Add 2 and 2."
+
+
+def test_set_span_reasoning_respects_capture_flag():
+ """Shared setter is a no-op when content capture is disabled."""
+ tracer, exporter = _tracer_with_exporter()
+ scope = SimpleNamespace(_reasoning_content="hidden")
+
+ with tracer.start_as_current_span("test") as span:
+ set_span_reasoning_content(span, scope, capture_message_content=False)
+ append_scope_reasoning(scope, " more")
+ set_span_reasoning_content(span, scope, capture_message_content=True)
+
+ finished = exporter.get_finished_spans()
+ assert len(finished) == 1
+ assert finished[0].attributes.get(REASONING_ATTR) == "hidden more"