def _get_valid_trace_context(trace_context):
    """
    Check if tarce_context is a valid type and returns a TraceContext object.
    """
    if not isinstance(trace_context, (TraceContext, dict)):
        raise TypeError(
            "The trace context has a value that is not a dictionary"
            f" (was {type(trace_context)})."
        )

    # Pretty much abuse the dynamic nature of python to make it possible to support
    # both dict and TraceContext types.
    if isinstance(trace_context, dict):
        trace_context = TraceContext(**trace_context)

    return trace_context

    Check if tarce_context is a valid type and returns a TraceContext object.
    
st2common/st2common/services/trace.py