def traced_stt(func: Optional[Callable] = None, *, name: Optional[str] = None) -> Callable:
    """Traces STT service methods with transcription attributes.

    Automatically captures and records:
    - Service name and model information
    - Transcription text and final status
    - Language information
    - Performance metrics like TTFB

    Args:
        func: The STT method to trace.
        name: Custom span name. Defaults to function name.

    Returns:
        Wrapped method with STT-specific tracing.
    """
    if not is_tracing_available():
        return _noop_decorator if func is None else _noop_decorator(func)

    def decorator(f):
        @functools.wraps(f)
        async def wrapper(self, transcript, is_final, language=None):
            try:
                if not is_tracing_available():
                    return await f(self, transcript, is_final, language)

                service_class_name = self.__class__.__name__
                span_name = "stt"

                # Get the turn context first, then fall back to service context
                turn_context = get_current_turn_context()
                parent_context = turn_context or _get_parent_service_context(self)

                # Create a new span as child of the turn span or service span
                tracer = trace.get_tracer("pipecat")
                with tracer.start_as_current_span(
                    span_name, context=parent_context
                ) as current_span:
                    try:
                        # Get TTFB metric if available
                        ttfb: Optional[float] = getattr(
                            getattr(self, "_metrics", None), "ttfb", None
                        )

                        # Use settings from the service if available
                        settings = getattr(self, "_settings", {})

                        add_stt_span_attributes(
                            span=current_span,
                            service_name=service_class_name,
                            model=getattr(self, "model_name", settings.get("model", "unknown")),
                            transcript=transcript,
                            is_final=is_final,
                            language=str(language) if language else None,
                            vad_enabled=getattr(self, "vad_enabled", False),
                            settings=settings,
                            ttfb=ttfb,
                        )

                        # Call the original function
                        return await f(self, transcript, is_final, language)
                    except Exception as e:
                        # Log any exception but don't disrupt the main flow
                        logging.warning(f"Error in STT transcription tracing: {e}")
                        raise
            except Exception as e:
                logging.error(f"Error in STT tracing (continuing without tracing): {e}")
                # If tracing fails, fall back to the original function
                return await f(self, transcript, is_final, language)

        return wrapper

    if func is not None:
        return decorator(func)
    return decorator
Traces STT service methods with transcription attributes.

    Automatically captures and records:
    - Service name and model information
    - Transcription text and final status
    - Language information
    - Performance metrics like TTFB

    Args:
        func: The STT method to trace.
        name: Custom span name. Defaults to function name.

    Returns:
        Wrapped method with STT-specific tracing.
    
src/pipecat/utils/tracing/service_decorators.py