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) –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
141 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 | |
_get_build_args
property
¶
_get_build_args
Build kwargs for fetching prompt from Langfuse based on load_args and sync_policy.
This is a cached property that computes the arguments once and reuses them for performance optimization, as these parameters are accessed frequently during load operations and error message generation.
When sync_policy="local", load_args (version/label) are ignored since local files are the source of truth. Users get a warning and the latest version is fetched for synchronization purposes only.
When sync_policy="remote" or "strict", load_args are respected since remote versions matter for these policies.
Returns:
-
dict[str, Any]–Cached kwargs dictionary for langfuse.get_prompt() with name, type, and
-
dict[str, Any]–optional version or label parameters.
_langfuse
instance-attribute
¶
_langfuse = Langfuse(
public_key=credentials["public_key"],
secret_key=credentials["secret_key"],
host=get("host"),
)
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.
_adapt_langfuse_chat_format ¶
_adapt_langfuse_chat_format(prompt_data)
Remove Langfuse-specific 'type' key from chat messages for local file compatibility.
Parameters:
-
prompt_data(str | list) –The prompt data from Langfuse (string or list of messages).
Returns:
-
str | list–New prompt data with 'type' key removed from messages if present.
-
str | list–For string prompts, returns the input unchanged.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
_describe ¶
_describe()
Return a description of the dataset for Kedro's internal use.
Returns:
-
dict[str, Any]–Dictionary containing dataset description with filepath and Langfuse prompt details.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
315 316 317 318 319 320 321 322 323 324 325 | |
_get_prompt_description ¶
_get_prompt_description()
Get consistent prompt description for error messages.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
327 328 329 | |
_sync_local_policy ¶
_sync_local_policy(local_data, langfuse_prompt)
Handle local sync policy - local file takes precedence.
Local files are the source of truth. When local content differs from remote, the local content is pushed to Langfuse as a new version. If local file is missing but remote exists, the remote content is saved locally.
Parameters:
-
local_data(str | None) –Content from local file, None if file doesn't exist
-
langfuse_prompt(Any | None) –Langfuse prompt object, None if not found remotely
Returns:
-
Any–Langfuse prompt object after syncing
Raises:
-
DatasetError–If neither local nor remote prompt exists
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
_sync_remote_policy ¶
_sync_remote_policy(local_data, langfuse_prompt)
Handle remote sync policy - Langfuse version takes precedence.
Parameters:
-
local_data(str | None) –Content from local file, None if file doesn't exist
-
langfuse_prompt(Any | None) –Langfuse prompt object, None if not found remotely
Returns:
-
Any–Langfuse prompt object after updating local file if needed
Raises:
-
DatasetError–If remote prompt doesn't exist
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
_sync_strict_policy ¶
_sync_strict_policy(local_data, langfuse_prompt)
Handle strict sync policy - error if local and remote differ.
Parameters:
-
local_data(str | list | None) –Content from local file, None if file doesn't exist.
-
langfuse_prompt(Any | None) –Langfuse prompt object, None if not found remotely.
Returns:
-
Any–Langfuse prompt object if sync is successful.
Raises:
-
DatasetError–If either local_data or langfuse_prompt is missing, or if they differ.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | |
_sync_with_langfuse ¶
_sync_with_langfuse(local_data, langfuse_prompt)
Synchronize local file and Langfuse prompt based on configured sync policy.
This method delegates to specialized sync policy handlers based on the configured sync_policy setting.
Parameters:
-
local_data(str | None) –Content from local file, None if file doesn't exist
-
langfuse_prompt(Any | None) –Langfuse prompt object, None if not found remotely
Returns:
-
Any–Langfuse prompt object after synchronization
Raises:
-
DatasetError–Based on sync_policy conflicts (see individual policy methods)
-
DatasetError–If no prompt found locally or in Langfuse
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
_validate_init_params ¶
_validate_init_params(
filepath,
credentials,
prompt_type,
sync_policy,
mode,
load_args=None,
save_args=None,
)
Validate initialization parameters.
Parameters:
-
filepath(str) –File path to validate for supported extensions.
-
credentials(dict[str, Any]) –Credentials dictionary to validate.
-
prompt_type(str) –Prompt type to validate.
-
sync_policy(str) –Sync policy to validate.
-
mode(str) –Mode to validate.
-
load_args(dict[str, Any] | None, default:None) –Load arguments to validate.
-
save_args(dict[str, Any] | None, default:None) –Save arguments to validate.
Raises:
-
DatasetError–If parameters are invalid or filepath has an unsupported extension.
-
ImportError–If langchain package is required but not available.
Source code in kedro_datasets_experimental/langfuse/prompt_dataset.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
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
581 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 | |
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
626 627 628 629 630 631 632 633 | |
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
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |