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

Function test_generate_content_span

tests/unittests/telemetry/test_spans.py:833–975  ·  view source on GitHub ↗

Test native generate_content span creation with attributes and logs.

(
    mock_guess_system_name,
    mock_tracer,
    mock_otel_logger,
    monkeypatch,
    env_capture_value,
    capture_content,
    user_id,
)

Source from the content-addressed store, hash-verified

831)
832@pytest.mark.parametrize('user_id', ['some-user-id', None])
833async def test_generate_content_span(
834 mock_guess_system_name,
835 mock_tracer,
836 mock_otel_logger,
837 monkeypatch,
838 env_capture_value,
839 capture_content,
840 user_id,
841):
842 """Test native generate_content span creation with attributes and logs."""
843 # Arrange
844 monkeypatch.setenv(
845 'OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT',
846 env_capture_value,
847 )
848 monkeypatch.setattr(
849 'google.adk.telemetry.tracing._instrumented_with_opentelemetry_instrumentation_google_genai',
850 lambda: False,
851 )
852
853 agent = LlmAgent(name='test_agent', model='not-a-gemini-model')
854 invocation_context = await _create_invocation_context(agent)
855 invocation_context.session.user_id = user_id
856 system_instruction = types.Content(
857 parts=[types.Part.from_text(text='You are a helpful assistant.')],
858 )
859 user_content1 = types.Content(role='user', parts=[types.Part(text='Hello')])
860 user_content2 = types.Content(role='user', parts=[types.Part(text='World')])
861
862 model_content = types.Content(
863 role='model', parts=[types.Part(text='Response')]
864 )
865
866 llm_request = LlmRequest(
867 model='some-model',
868 contents=[user_content1, user_content2],
869 config=types.GenerateContentConfig(system_instruction=system_instruction),
870 )
871 llm_response = LlmResponse(
872 content=model_content,
873 finish_reason=types.FinishReason.STOP,
874 usage_metadata=types.GenerateContentResponseUsageMetadata(
875 prompt_token_count=10,
876 candidates_token_count=20,
877 ),
878 )
879
880 model_response_event = mock.MagicMock()
881 model_response_event.id = 'event-123'
882
883 mock_span = (
884 mock_tracer.start_as_current_span.return_value.__enter__.return_value
885 )
886
887 # Act
888 async with use_inference_span(
889 llm_request, invocation_context, model_response_event
890 ) as gc_span:

Callers

nothing calls this directly

Calls 8

LlmAgentClass · 0.90
LlmRequestClass · 0.90
LlmResponseClass · 0.90
use_inference_spanFunction · 0.90
trace_inference_resultFunction · 0.90
model_dumpMethod · 0.45

Tested by

no test coverage detected