test(e2e): register the weather tool in the first realtime session update and read Langfuse v2 flattened attributes

This commit is contained in:
mateo-berri 2026-11-14 04:18:38 -08:00
parent 1523da8187
commit 7f9977e37e
3 changed files with 28 additions and 12 deletions

View file

@ -341,7 +341,7 @@ async def _run_audio_input_pipeline(
"When asked about the weather, always call the get_weather tool. "
"Never guess temperatures."
),
session_properties=SERVER_VAD_SETTINGS,
session_properties=SERVER_VAD_SETTINGS.model_copy(update={"tools": WEATHER_TOOL}),
),
)

View file

@ -164,19 +164,18 @@ class LangfuseObservationList(BaseModel):
data: list[LangfuseObservation] = []
class LangfuseOtelMetadata(BaseModel):
"""Langfuse stores every OTel span attribute under metadata.attributes."""
model_config = ConfigDict(extra="ignore")
attributes: dict[str, str] = {}
_FLATTENED_ATTRIBUTE_PREFIX: Final = "attributes."
def otel_attributes(obs: LangfuseObservation) -> dict[str, str]:
try:
return LangfuseOtelMetadata.model_validate(obs.metadata).attributes
except ValidationError:
return {}
def otel_attributes(obs: LangfuseObservation) -> dict[str, JsonValue]:
metadata: Final = obs.metadata if isinstance(obs.metadata, dict) else {}
nested: Final = metadata.get("attributes")
flattened: Final = {
key.removeprefix(_FLATTENED_ATTRIBUTE_PREFIX): value
for key, value in metadata.items()
if key.startswith(_FLATTENED_ATTRIBUTE_PREFIX)
}
return (nested if isinstance(nested, dict) else {}) | flattened
def is_otel_v2_generation(obs: LangfuseObservation, *, key_alias: str) -> bool:

View file

@ -38,6 +38,23 @@ def test_observation_preserves_v2_content_usage_and_cost(encoded: bool, cost: fl
assert not is_otel_v2_generation(observation, key_alias="another-key")
def test_otel_v2_generation_matches_metadata_flattened_by_the_v2_observations_api() -> None:
observation: Final = LangfuseObservation.model_validate(
{
"id": "observation",
"metadata": {
"scope.name": "litellm",
"attributes.langfuse.observation.type": "generation",
"attributes.litellm.metadata.user_api_key_alias": "test-key-alias",
"attributes.gen_ai.usage.input_tokens": 17,
},
}
)
assert is_otel_v2_generation(observation, key_alias="test-key-alias")
assert not is_otel_v2_generation(observation, key_alias="another-key")
def test_observation_preserves_plain_text_and_legacy_cost() -> None:
cost: Final = 0.000031
observation: Final = LangfuseObservation.model_validate(