Trace result of the inference in generate_content span.
(
invocation_context: InvocationContext | None,
span: Span | None | GenerateContentSpan,
llm_response: LlmResponse,
)
| 834 | |
| 835 | |
| 836 | def trace_inference_result( |
| 837 | invocation_context: InvocationContext | None, |
| 838 | span: Span | None | GenerateContentSpan, |
| 839 | llm_response: LlmResponse, |
| 840 | ): |
| 841 | """Trace result of the inference in generate_content span.""" |
| 842 | telemetry_config = _telemetry_config_from_invocation_context( |
| 843 | invocation_context |
| 844 | ) |
| 845 | gc_span = None |
| 846 | if isinstance(span, GenerateContentSpan): |
| 847 | gc_span = span |
| 848 | span = gc_span.span |
| 849 | |
| 850 | if span is None: |
| 851 | return |
| 852 | |
| 853 | if llm_response.partial: |
| 854 | return |
| 855 | |
| 856 | if finish_reason := llm_response.finish_reason: |
| 857 | span.set_attribute(GEN_AI_RESPONSE_FINISH_REASONS, [finish_reason.lower()]) |
| 858 | _set_usage_metadata_attributes(span, llm_response.usage_metadata) |
| 859 | |
| 860 | if telemetry_config.should_use_experimental_genai_semconv and isinstance( |
| 861 | gc_span, GenerateContentSpan |
| 862 | ): |
| 863 | set_operation_details_attributes_from_response( |
| 864 | llm_response, |
| 865 | gc_span.operation_details_attributes, |
| 866 | gc_span.operation_details_common_attributes, |
| 867 | ) |
| 868 | |
| 869 | else: |
| 870 | otel_logger.emit( |
| 871 | LogRecord( |
| 872 | event_name=GEN_AI_CHOICE_EVENT, |
| 873 | body=choice_body( |
| 874 | llm_response, telemetry_config or TelemetryConfig() |
| 875 | ), |
| 876 | attributes={GEN_AI_SYSTEM: _guess_gemini_system_name()}, |
| 877 | ) |
| 878 | ) |
| 879 | |
| 880 | |
| 881 | def _guess_gemini_system_name() -> str: |