Create a new trace. The trace will not be started automatically; you should either use it as a context manager (`with trace(...):`) or call `trace.start()` + `trace.finish()` manually. In addition to the workflow name and optional grouping identifier, you can provide an arbitra
(
workflow_name: str,
trace_id: str | None = None,
group_id: str | None = None,
metadata: dict[str, Any] | None = None,
tracing: TracingConfig | None = None,
disabled: bool = False,
)
| 29 | |
| 30 | |
| 31 | def trace( |
| 32 | workflow_name: str, |
| 33 | trace_id: str | None = None, |
| 34 | group_id: str | None = None, |
| 35 | metadata: dict[str, Any] | None = None, |
| 36 | tracing: TracingConfig | None = None, |
| 37 | disabled: bool = False, |
| 38 | ) -> Trace: |
| 39 | """ |
| 40 | Create a new trace. The trace will not be started automatically; you should either use |
| 41 | it as a context manager (`with trace(...):`) or call `trace.start()` + `trace.finish()` |
| 42 | manually. |
| 43 | |
| 44 | In addition to the workflow name and optional grouping identifier, you can provide |
| 45 | an arbitrary metadata dictionary to attach additional user-defined information to |
| 46 | the trace. |
| 47 | |
| 48 | Args: |
| 49 | workflow_name: The name of the logical app or workflow. For example, you might provide |
| 50 | "code_bot" for a coding agent, or "customer_support_agent" for a customer support agent. |
| 51 | trace_id: The ID of the trace. Optional. If not provided, we will generate an ID. We |
| 52 | recommend using `util.gen_trace_id()` to generate a trace ID, to guarantee that IDs are |
| 53 | correctly formatted. |
| 54 | group_id: Optional grouping identifier to link multiple traces from the same conversation |
| 55 | or process. For instance, you might use a chat thread ID. |
| 56 | metadata: Optional dictionary of additional metadata to attach to the trace. |
| 57 | tracing: Optional tracing configuration for exporting this trace. |
| 58 | disabled: If True, we will return a Trace but the Trace will not be recorded. |
| 59 | |
| 60 | Returns: |
| 61 | The newly created trace object. |
| 62 | """ |
| 63 | current_trace = get_trace_provider().get_current_trace() |
| 64 | if current_trace: |
| 65 | logger.warning( |
| 66 | "Trace already exists. Creating a new trace, but this is probably a mistake." |
| 67 | ) |
| 68 | |
| 69 | return get_trace_provider().create_trace( |
| 70 | name=workflow_name, |
| 71 | trace_id=trace_id, |
| 72 | group_id=group_id, |
| 73 | metadata=metadata, |
| 74 | tracing=tracing, |
| 75 | disabled=disabled, |
| 76 | ) |
| 77 | |
| 78 | |
| 79 | def get_current_trace() -> Trace | None: |