MCPcopy Create free account
hub / github.com/google/adk-python / test_trace_call_llm

Function test_trace_call_llm

tests/unittests/telemetry/test_spans.py:155–221  ·  view source on GitHub ↗

Test trace_call_llm sets all telemetry attributes correctly with normal content.

(monkeypatch, mock_span_fixture)

Source from the content-addressed store, hash-verified

153
154@pytest.mark.asyncio
155async def test_trace_call_llm(monkeypatch, mock_span_fixture):
156 """Test trace_call_llm sets all telemetry attributes correctly with normal content."""
157 monkeypatch.setattr(
158 'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
159 )
160
161 agent = LlmAgent(name='test_agent')
162 invocation_context = await _create_invocation_context(agent)
163 llm_request = LlmRequest(
164 model='gemini-pro',
165 contents=[
166 types.Content(
167 role='user',
168 parts=[types.Part(text='Hello, how are you?')],
169 ),
170 ],
171 config=types.GenerateContentConfig(
172 top_p=0.95,
173 max_output_tokens=1024,
174 thinking_config=types.ThinkingConfig(thinking_budget=10),
175 ),
176 )
177 llm_response = LlmResponse(
178 turn_complete=True,
179 finish_reason=types.FinishReason.STOP,
180 usage_metadata=types.GenerateContentResponseUsageMetadata(
181 total_token_count=100,
182 prompt_token_count=50,
183 candidates_token_count=50,
184 thoughts_token_count=10,
185 ),
186 )
187 # We dynamically assign system_instruction_tokens rather than passing it
188 # to the GenerateContentResponseUsageMetadata constructor to ensure backward
189 # compatibility with older versions of the google-genai SDK that do not have
190 # this property defined in their Pydantic models.
191 try:
192 llm_response.usage_metadata.system_instruction_tokens = 5
193 except Exception:
194 pass
195
196 trace_call_llm(invocation_context, 'test_event_id', llm_request, llm_response)
197
198 expected_calls = [
199 mock.call('gen_ai.system', 'gcp.vertex.agent'),
200 mock.call('gen_ai.request.top_p', 0.95),
201 mock.call('gen_ai.request.max_tokens', 1024),
202 mock.call('gcp.vertex.agent.llm_response', mock.ANY),
203 mock.call('gen_ai.usage.experimental.reasoning_tokens_limit', 10),
204 mock.call('gen_ai.response.finish_reasons', ['stop']),
205 ]
206
207 expected_usage_attrs = {
208 'gen_ai.usage.input_tokens': 50,
209 'gen_ai.usage.output_tokens': 60,
210 'gen_ai.usage.reasoning.output_tokens': 10,
211 }
212 if hasattr(llm_response.usage_metadata, 'system_instruction_tokens'):

Callers

nothing calls this directly

Calls 7

LlmAgentClass · 0.90
LlmRequestClass · 0.90
LlmResponseClass · 0.90
trace_call_llmFunction · 0.90
callMethod · 0.45

Tested by

no test coverage detected