langfuse.PromptDataset
kedro_datasets_experimental.langfuse.PromptDataset ¶
PromptDataset(
filepath,
prompt_name,
credentials,
prompt_type="text",
sync_policy="local",
mode="sdk",
load_args=None,
save_args=None,
)
Bases: AbstractDataset
Kedro dataset for managing prompts with Langfuse versioning and synchronization.
This dataset provides seamless integration between local prompt files (JSON/YAML) and Langfuse prompt management, supporting version control, labelling, and different synchronization policies.
On save / load behaviour:
- On save: Creates a new version of the prompt in Langfuse with the local data.
- On load: Synchronizes based on
sync_policyand returns a raw Langfuse object (SDK mode) or a LangChainChatPromptTemplate(langchain mode).
Sync policies:
- local: Local file takes precedence (default).
load_args(version/label) are ignored with a warning, and the latest prompt from Langfuse is loaded if available, since local files are the source of truth. - remote: Langfuse version takes precedence.
load_argsare respected. - strict: Raises an error if local and remote differ.
load_argsare respected.
Examples:
Using catalog YAML configuration:
# Local sync policy - local files are source of truth
intent_prompt:
type: kedro_datasets_experimental.langfuse.PromptDataset
filepath: data/prompts/intent.json
prompt_name: "intent-classifier"
prompt_type: "chat"
credentials: langfuse_credentials
sync_policy: local
mode: langchain
# load_args are ignored in local mode with warning
# and latest prompt from langfuse is loaded if available
save_args:
labels: ["staging", "v2.1"]
# Remote sync policy - Langfuse versions are source of truth
production_prompt:
type: kedro_datasets_experimental.langfuse.PromptDataset
filepath: data/prompts/production.json
prompt_name: "intent-classifier"
sync_policy: remote
load_args:
label: "production" # This is respected in remote mode
Using Python API:
from kedro_datasets_experimental.langfuse import PromptDataset
# Basic usage (using default Langfuse cloud)
dataset = PromptDataset(
filepath="data/prompts/intent.json",
prompt_name="intent-classifier",
prompt_type="chat",
credentials={
"public_key": "pk_...",
"secret_key": "sk_...", # pragma: allowlist secret
},
)
# With custom host
dataset = PromptDataset(
filepath="data/prompts/intent.json",
prompt_name="intent-classifier",
prompt_type="chat",
mode="langchain",
credentials={
"public_key": "pk_...",
"secret_key": "sk_...", # pragma: allowlist secret
"host": "https://custom.langfuse.com",
},
)
# Load and use prompt
prompt_template = dataset.load()
formatted = prompt_template.format(user_input="Hello world")
# Save new version with labels
chat_prompt = [
{"type": "chatmessage", "role": "system", "content": "You are helpful."},
{"type": "chatmessage", "role": "human", "content": "{input}"},
]
dataset.save(chat_prompt)
Parameters:
-
filepath(str | PathLike) –Local file path for storing prompt. Supports .json, .yaml, .yml extensions.
-
prompt_name(str) –Unique identifier for the prompt in Langfuse.
-
prompt_type(Literal['chat', 'text'], default:'text') –Type of prompt - "chat" for conversation or "text" for single prompts.
-
credentials(dict[str, Any]) –Dictionary with Langfuse credentials. Required: {public_key, secret_key}. Optional: {host} (defaults to Langfuse cloud if not provided).
-
sync_policy(Literal['local', 'remote', 'strict'], default:'local') –How to handle conflicts between local and remote: - "local": Local file takes precedence (default) - "remote": Langfuse version takes precedence - "strict": Error if local and remote differ
-
mode(Literal['langchain', 'sdk'], default:'sdk') –Return type for load() method: - "sdk": Returns raw Langfuse prompt object (default) - "langchain": Returns ChatPromptTemplate object
-
load_args(dict[str, Any] | None, default:None) –Dictionary with loading parameters. Only used when sync_policy="remote" or "strict". Ignored with warning when sync_policy="local". Supported keys: - version (int): Specific version number to load - label (str): Specific label to load (e.g., "production", "staging") Note: Langfuse will throw an error if both version and label are used together. So label is preferred over version if provided.
-
save_args(dict[str, Any] | None, default:None) –Dictionary with saving parameters. Supported keys: - labels (list[str]): List of labels to assign to new prompt versions
Examples:
>>> # Local sync policy (default) - local files are source of truth
>>> dataset = PromptDataset(
... filepath="prompts/intent.json",
... prompt_name="intent-classifier",
... credentials={"public_key": "pk_...", "secret_key": "sk_..."} # pragma: allowlist secret
... )
>>> # Remote sync policy - load specific version from Langfuse
>>> dataset = PromptDataset(
... filepath="prompts/intent.yaml",
... prompt_name="intent-classifier",
... credentials=creds,
... sync_policy="remote",
... load_args={"version": 3} # This is respected in remote mode
... )
>>> # Remote sync policy - load specific label from Langfuse
>>> dataset = PromptDataset(
... filepath="prompts/production.json",
... prompt_name="intent-classifier",
... credentials=creds,
... sync_policy="remote",
... load_args={"label": "production"} # This is respected in remote mode
... )
>>> # With custom host
>>> dataset = PromptDataset(
... filepath="prompts/intent.json",
... prompt_name="intent-classifier",
... credentials={"public_key": "pk_...", "secret_key": "sk_...", "host": "https://custom.langfuse.com"} # pragma: allowlist secret
... )
>>> # Auto-label new versions when saving (works with any sync policy)
>>> dataset = PromptDataset(
... filepath="prompts/intent.json",
... prompt_name="intent-classifier",
... credentials=creds,
... save_args={"labels": ["staging", "v2.1"]}
... )
Raises:
-
DatasetError–If credentials are missing required keys or filepath has an unsupported extension.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
file_dataset
property
¶
file_dataset
Get appropriate Kedro dataset based on file extension (cached).
Returns:
-
Union[JSONDataset, YAMLDataset]–JSONDataset for .json files, YAMLDataset for .yaml/.yml files.
load ¶
load()
Loads prompt from Langfuse, local file if present, and synchronizes based on sync_policy. Returns prompt in format specified by mode.
Returns:
-
ChatPromptTemplate–If mode="langchain", ready-to-use LangChain template.
-
Any–If mode="sdk", raw Langfuse prompt object.
Raises:
-
DatasetError–If sync_policy conflicts or no prompt found.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | |
preview ¶
preview()
Generate a JSON-compatible preview of the local prompt data for Kedro-Viz.
Returns:
-
JSONPreview–Serialised JSON string for Kedro-Viz. Returns a descriptive message if the local file does not exist.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
627 628 629 630 631 632 633 634 | |
save ¶
save(data)
Create a new version of prompt in Langfuse with the local data.
Parameters:
-
data(str | list) –The prompt content to save. Can be string for text prompts or list of message dictionaries for chat prompts.
Raises:
-
DatasetError–If Langfuse API call fails or invalid data format.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |