Skip to content

Commit 62ee78b

Browse files
committed
Fix rubocop offenses and test failures
- Use attr_reader for tracing_langsmith_compat (custom setter defined) - Consolidate build_request_attributes params into config hash - Use allow/have_received pattern for logger spy tests - Fix Model::Info double in tests
1 parent 3667cb4 commit 62ee78b

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

lib/ruby_llm/chat.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ def complete_with_span(span, &)
168168
model: @model,
169169
provider: @provider.slug,
170170
session_id: @session_id,
171-
temperature: @temperature,
172-
metadata: @metadata,
173-
langsmith_compat: langsmith,
174-
metadata_prefix: @config.tracing_metadata_prefix
171+
config: {
172+
temperature: @temperature,
173+
metadata: @metadata,
174+
langsmith_compat: langsmith,
175+
metadata_prefix: @config.tracing_metadata_prefix
176+
}
175177
)
176178
)
177179

lib/ruby_llm/configuration.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class Configuration
5151
:tracing_enabled,
5252
:tracing_log_content,
5353
:tracing_max_content_length,
54-
:tracing_metadata_prefix,
55-
:tracing_langsmith_compat
54+
:tracing_metadata_prefix
55+
56+
attr_reader :tracing_langsmith_compat
5657

5758
def initialize
5859
@request_timeout = 300

lib/ruby_llm/instrumentation.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,16 @@ def build_completion_attributes(message, max_length:, langsmith_compat: false)
153153
attrs
154154
end
155155

156-
def build_request_attributes(model:, provider:, session_id:, temperature: nil, metadata: nil,
157-
langsmith_compat: false, metadata_prefix: 'metadata')
156+
def build_request_attributes(model:, provider:, session_id:, config: {})
158157
attrs = {
159158
'gen_ai.system' => provider.to_s,
160159
'gen_ai.operation.name' => 'chat',
161160
'gen_ai.request.model' => model.id,
162161
'gen_ai.conversation.id' => session_id
163162
}
164-
attrs['langsmith.span.kind'] = 'LLM' if langsmith_compat
165-
attrs['gen_ai.request.temperature'] = temperature if temperature
166-
build_metadata_attributes(attrs, metadata, prefix: metadata_prefix) if metadata
163+
attrs['langsmith.span.kind'] = 'LLM' if config[:langsmith_compat]
164+
attrs['gen_ai.request.temperature'] = config[:temperature] if config[:temperature]
165+
build_metadata_attributes(attrs, config[:metadata], prefix: config[:metadata_prefix]) if config[:metadata]
167166
attrs
168167
end
169168

spec/ruby_llm/instrumentation_spec.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,23 @@
9595
RubyLLM.configure { |c| c.tracing_enabled = true }
9696
described_class.reset!
9797
allow(described_class).to receive(:otel_available?).and_return(false)
98+
allow(RubyLLM.logger).to receive(:warn)
9899

99-
expect(RubyLLM.logger).to receive(:warn).with(/OpenTelemetry is not available/)
100100
expect(described_class.enabled?).to be false
101+
expect(RubyLLM.logger).to have_received(:warn).with(/OpenTelemetry is not available/)
101102
end
102103

103104
it 'only warns once per reset cycle' do
104105
RubyLLM.configure { |c| c.tracing_enabled = true }
105106
described_class.reset!
106107
allow(described_class).to receive(:otel_available?).and_return(false)
108+
allow(RubyLLM.logger).to receive(:warn)
107109

108-
expect(RubyLLM.logger).to receive(:warn).once
109110
described_class.enabled?
110111
described_class.enabled?
111112
described_class.enabled?
113+
114+
expect(RubyLLM.logger).to have_received(:warn).once
112115
end
113116
end
114117

@@ -419,7 +422,7 @@
419422
end
420423

421424
describe '.build_request_attributes' do
422-
let(:model) { instance_double(RubyLLM::Model, id: 'gpt-4') }
425+
let(:model) { instance_double(RubyLLM::Model::Info, id: 'gpt-4') }
423426

424427
it 'does not include langsmith.span.kind by default' do
425428
attrs = RubyLLM::Instrumentation::SpanBuilder.build_request_attributes(
@@ -438,11 +441,22 @@
438441
model: model,
439442
provider: :openai,
440443
session_id: 'session-123',
441-
langsmith_compat: true
444+
config: { langsmith_compat: true }
442445
)
443446

444447
expect(attrs['langsmith.span.kind']).to eq 'LLM'
445448
end
449+
450+
it 'includes temperature when provided' do
451+
attrs = RubyLLM::Instrumentation::SpanBuilder.build_request_attributes(
452+
model: model,
453+
provider: :openai,
454+
session_id: 'session-123',
455+
config: { temperature: 0.7 }
456+
)
457+
458+
expect(attrs['gen_ai.request.temperature']).to eq 0.7
459+
end
446460
end
447461

448462
describe '.build_metadata_attributes' do

0 commit comments

Comments
 (0)