元数据
Observation(参见 Litefuse 数据模型)可以通过元数据进行丰富,帮助你更好地理解你的应用,并在 Litefuse 中关联 observation。
你可以在 Litefuse UI 和 API 中按元数据键过滤。
传播的元数据
使用 propagate_attributes() 让元数据在某个上下文内自动应用到所有 observation 上。传播的元数据是键值对,值最长 200 个字符的字符串。键只能由字母数字组成。如果元数据值超过 200 个字符,将被丢弃。
使用 @observe() 装饰器时:
from langfuse import observe, propagate_attributes
@observe()
def process_data():
# Propagate metadata to all child observations
with propagate_attributes(
metadata={"source": "api", "region": "us-east-1", "user_tier": "premium"}
):
# All nested observations automatically inherit this metadata
result = perform_processing()
return result直接创建 observation 时:
from langfuse import get_client, propagate_attributes
langfuse = get_client()
with langfuse.start_as_current_observation(as_type="span", name="process-request") as root_span:
# Propagate metadata to all child observations
with propagate_attributes(metadata={"request_id": "req_12345", "region": "us-east-1"}):
# All observations created here automatically have this metadata
with root_span.start_as_current_observation(
as_type="generation",
name="generate-response",
model="gpt-4o"
) as gen:
# This generation automatically has the metadata
passNote on Attribute Propagation
We use Attribute Propagation to propagate `metadata` across all observations of a trace. We will use all observations with `metadata` to create `metadata`-level metrics. Please consider the following when using Attribute Propagation:
- Values must be strings ≤200 characters
- Metadata keys: Alphanumeric characters only (no whitespace or special characters)
- Call early in your trace to ensure all observations are covered. This way you make sure that all Metrics in Litefuse are accurate.
- Invalid values are dropped with a warning
Learn more: Python SDK | TypeScript SDK
非传播的元数据
你也可以仅给特定 observation 添加元数据:
# Python SDK
from langfuse import get_client
langfuse = get_client()
with langfuse.start_as_current_observation(as_type="span", name="process-request") as root_span:
# Add metadata to this specific observation only
root_span.update(metadata={"stage": "parsing"})
# ... or access span via the current context
langfuse.update_current_span(metadata={"stage": "parsing"})这个页面对你有帮助吗?