Skip to content

Commit bbe629a

Browse files
committed
feat(skills): add discovery and availability tracing
1 parent 1eb1be3 commit bbe629a

22 files changed

Lines changed: 897 additions & 22 deletions

‎README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,35 @@ with langfuse.start_as_current_observation(as_type="span", name="process-request
5454
langfuse.flush()
5555
```
5656

57+
## Skills
58+
59+
Attach skills to spans to display them on generation observations in Langfuse.
60+
61+
```python
62+
from langfuse import Langfuse, discover_skills, propagate_attributes
63+
64+
skills = discover_skills(dir=".agents/skills")
65+
langfuse = Langfuse(skills={"available": skills})
66+
with propagate_attributes(attach_skills=True):
67+
...
68+
```
69+
70+
Provide skills directly to override the configured list without an attachment flag.
71+
72+
```python
73+
with propagate_attributes(skills_available=[{"name": "refund-policy", "version": 2}]):
74+
...
75+
```
76+
77+
Discovery reads `langfuse-skills.lock` from the skills directory, falling back to the working directory.
78+
79+
Fetch skill contents through the generated API.
80+
81+
```python
82+
skill = langfuse.api.unstable.skills.get("refund-policy", label="production", include_content=True)
83+
skill = await langfuse.async_api.unstable.skills.get("refund-policy", include_content=True)
84+
```
85+
5786
## Docs
5887

5988
- SDK guide: https://langfuse.com/docs/observability/sdk/overview

‎langfuse/__init__.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
from ._client.get_client import get_client
6666
from ._client.observe import observe
6767
from ._client.propagation import propagate_attributes
68+
from ._client.skills import discover_skills
6869
from ._client.span import (
6970
LangfuseAgent,
7071
LangfuseChain,
@@ -93,6 +94,8 @@
9394
OtelSpanData,
9495
OtelSpanIdentifier,
9596
OtelSpanPatch,
97+
SkillReference,
98+
SkillsConfig,
9699
)
97100

98101
Langfuse = _client_module.Langfuse
@@ -104,6 +107,9 @@
104107
"get_client",
105108
"observe",
106109
"propagate_attributes",
110+
"discover_skills",
111+
"SkillReference",
112+
"SkillsConfig",
107113
"ObservationTypeLiteral",
108114
"LangfuseSpan",
109115
"LangfuseGeneration",

‎langfuse/_client/attributes.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
ObservationTypeGenerationLike,
1919
ObservationTypeSpanLike,
2020
)
21+
from langfuse._client.skills import serialize_available_skills
2122
from langfuse._utils.serializer import EventSerializer
2223
from langfuse.api import MapValue
2324
from langfuse.logger import langfuse_logger
2425
from langfuse.model import PromptClient
25-
from langfuse.types import SpanLevel
26+
from langfuse.types import SkillReference, SpanLevel
2627

2728

2829
class LangfuseOtelSpanAttributes:
@@ -41,6 +42,7 @@ class LangfuseOtelSpanAttributes:
4142
OBSERVATION_STATUS_MESSAGE = "langfuse.observation.status_message"
4243
OBSERVATION_INPUT = "langfuse.observation.input"
4344
OBSERVATION_OUTPUT = "langfuse.observation.output"
45+
OBSERVATION_SKILLS_AVAILABLE = "langfuse.observation.skills_available"
4446

4547
# Langfuse-observation of type Generation attributes
4648
OBSERVATION_COMPLETION_START_TIME = "langfuse.observation.completion_start_time"
@@ -87,6 +89,7 @@ def create_trace_attributes(
8789
def create_span_attributes(
8890
*,
8991
metadata: Optional[Any] = None,
92+
available_skills: Optional[list[SkillReference]] = None,
9093
input: Optional[Any] = None,
9194
output: Optional[Any] = None,
9295
level: Optional[SpanLevel] = None,
@@ -103,6 +106,7 @@ def create_span_attributes(
103106
LangfuseOtelSpanAttributes.VERSION: version,
104107
LangfuseOtelSpanAttributes.OBSERVATION_INPUT: _serialize(input),
105108
LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT: _serialize(output),
109+
**create_available_skills_attributes(available_skills),
106110
**_flatten_and_serialize_metadata(metadata, "observation"),
107111
}
108112

@@ -114,6 +118,7 @@ def create_generation_attributes(
114118
name: Optional[str] = None,
115119
completion_start_time: Optional[datetime] = None,
116120
metadata: Optional[Any] = None,
121+
available_skills: Optional[list[SkillReference]] = None,
117122
level: Optional[SpanLevel] = None,
118123
status_message: Optional[str] = None,
119124
version: Optional[str] = None,
@@ -148,12 +153,22 @@ def create_generation_attributes(
148153
LangfuseOtelSpanAttributes.OBSERVATION_MODEL_PARAMETERS: _serialize(
149154
model_parameters
150155
),
156+
**create_available_skills_attributes(available_skills),
151157
**_flatten_and_serialize_metadata(metadata, "observation"),
152158
}
153159

154160
return {k: v for k, v in attributes.items() if v is not None}
155161

156162

163+
def create_available_skills_attributes(
164+
available_skills: Optional[list[SkillReference]],
165+
) -> dict[str, str]:
166+
serialized = serialize_available_skills(available_skills)
167+
if serialized is None:
168+
return {}
169+
return {LangfuseOtelSpanAttributes.OBSERVATION_SKILLS_AVAILABLE: serialized}
170+
171+
157172
def _serialize(obj: Any) -> Optional[str]:
158173
if obj is None or isinstance(obj, str):
159174
return obj

0 commit comments

Comments
 (0)