Skip to content

Commit 74e06eb

Browse files
committed
Fix instrumentation: style, empty metadata check, streaming tool spans
- Add blank line after SpanKind module for consistent style - Use metadata&.any? to avoid iterating empty hash - Skip tool span creation during streaming to prevent orphan spans
1 parent 878c357 commit 74e06eb

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/ruby_llm/chat.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def initialize(model: nil, provider: nil, assume_model_exists: false, context: n
1919
@session_id = session_id || SecureRandom.uuid
2020
@metadata = {}
2121
@temperature = nil
22+
@streaming_active = false
2223
@messages = []
2324
@tools = {}
2425
@params = {}
@@ -140,6 +141,8 @@ def complete(&)
140141
private
141142

142143
def complete_without_instrumentation(&)
144+
@streaming_active = block_given?
145+
143146
response = @provider.complete(
144147
messages,
145148
tools: @tools,
@@ -152,6 +155,8 @@ def complete_without_instrumentation(&)
152155
)
153156

154157
finalize_response(response, &)
158+
ensure
159+
@streaming_active = false
155160
end
156161

157162
def complete_with_span(span, &)
@@ -292,11 +297,19 @@ def handle_tool_calls(response, &) # rubocop:disable Metrics/PerceivedComplexity
292297
end
293298

294299
def execute_tool(tool_call)
300+
# Skip instrumentation for streaming (tool spans would be orphaned without parent chat span)
301+
return execute_tool_without_instrumentation(tool_call) if @streaming_active
302+
295303
Instrumentation.tracer.in_span('ruby_llm.tool', kind: Instrumentation::SpanKind::INTERNAL) do |span|
296304
execute_tool_with_span(tool_call, span)
297305
end
298306
end
299307

308+
def execute_tool_without_instrumentation(tool_call)
309+
tool = tools[tool_call.name.to_sym]
310+
tool.call(tool_call.arguments)
311+
end
312+
300313
def execute_tool_with_span(tool_call, span)
301314
tool = tools[tool_call.name.to_sym]
302315
args = tool_call.arguments

lib/ruby_llm/instrumentation.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module SpanKind
1111
CLIENT = :client
1212
INTERNAL = :internal
1313
end
14+
1415
class << self
1516
def enabled?
1617
return false unless RubyLLM.config.tracing_enabled
@@ -157,7 +158,7 @@ def build_request_attributes(model:, provider:, session_id:, temperature: nil, m
157158
'gen_ai.conversation.id' => session_id
158159
}
159160
attrs['gen_ai.request.temperature'] = temperature if temperature
160-
build_metadata_attributes(attrs, metadata) if metadata
161+
build_metadata_attributes(attrs, metadata) if metadata&.any?
161162
attrs
162163
end
163164

0 commit comments

Comments
 (0)