opik.PromptDataset
kedro_datasets_experimental.opik.PromptDataset ¶
PromptDataset(
filepath,
prompt_name,
credentials,
prompt_type="text",
sync_policy="local",
mode="sdk",
load_args=None,
save_args=None,
**opik_kwargs
)
Bases: AbstractDataset
Kedro dataset for managing prompts with Opik versioning and synchronisation.
This dataset provides seamless integration between local prompt files (JSON/YAML) and Opik's prompt management system, supporting version control, metadata tracking, and different synchronisation policies.
On save / load behaviour:
- On save: Creates a new version of the prompt in Opik with the local data.
- On load: Synchronises based on
sync_policyand returns either the raw Opik object (SDK mode) or a LangChainChatPromptTemplate(LangChain mode).
Sync policies:
- local: Local file takes precedence (default).
load_argsare ignored with a warning, since local files are the source of truth. - remote: Opik version takes precedence.
load_argsare respected if supported. - strict: Raises an error if local and remote differ.
load_argsare respected if supported
Examples:
Using catalog YAML configuration:
# Local sync policy - local files are source of truth
customer_prompt:
type: kedro_datasets_experimental.opik.PromptDataset
filepath: data/prompts/customer.json
prompt_name: customer_support_v1
prompt_type: chat
credentials: opik_credentials
sync_policy: local
mode: langchain
# Remote sync policy - Opik versions are source of truth
production_prompt:
type: kedro_datasets_experimental.opik.PromptDataset
filepath: data/prompts/production.yaml
prompt_name: customer_support_v1
sync_policy: remote
mode: sdk
Using Python API:
from kedro_datasets_experimental.opik import PromptDataset
# Create dataset for chat prompt
dataset = PromptDataset(
filepath="data/prompts/customer_support.json",
prompt_name="customer_support_v1",
prompt_type="chat",
credentials={
"api_key": "opik_...", # pragma: allowlist secret
"workspace": "my-workspace",
},
)
# Load prompt as LangChain ChatPromptTemplate
prompt_template = dataset.load()
formatted = prompt_template.format(question="How are you?")
# Save with metadata
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, {question}"},
]
dataset.save(messages)
Parameters:
-
filepath(str) –Local file path for storing prompt. Supports .json, .yaml, .yml extensions.
-
prompt_name(str) –Unique identifier for the prompt in Opik.
-
prompt_type(Literal['chat', 'text'], default:'text') –Either "chat" for message-based prompts or "text" for simple strings.
-
sync_policy(Literal['local', 'remote', 'strict'], default:'local') –How to handle conflicts between local and remote: - "local": Local file takes precedence (default) - "remote": Opik version takes precedence - "strict": Error if local and remote differ
-
mode(Literal['langchain', 'sdk'], default:'sdk') –Return type for load() method: - "sdk": Returns raw Opik prompt object (default) - "langchain": Returns ChatPromptTemplate object
-
credentials(dict[str, Any]) –Dictionary with Opik client configuration. Keys may include 'api_key', 'workspace', 'project_name', etc.
-
load_args(dict[str, Any] | None, default:None) –Dictionary with loading parameters. Currently reserved for future use when Opik supports version/label retrieval.
-
save_args(dict[str, Any] | None, default:None) –Dictionary with saving parameters. Keys may include: - metadata: Additional metadata to store with the prompt
-
**opik_kwargs(Any, default:{}) –Additional kwargs passed to Opik client initialisation.
Raises:
-
DatasetError–If required parameters are missing or invalid.
-
ImportError–If langchain is required but not installed.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 | |
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.
_convert_to_langchain_template ¶
_convert_to_langchain_template(prompt_data)
Convert prompt data to LangChain ChatPromptTemplate.
Parameters:
-
prompt_data(str | list | None) –Raw prompt data (string or list of messages).
Returns:
-
ChatPromptTemplate–ChatPromptTemplate ready for use in LangChain pipelines.
Raises:
-
DatasetError–If prompt data format is invalid.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
_describe ¶
_describe()
Return a description of the dataset for Kedro's internal use.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
250 251 252 253 254 255 256 257 258 | |
_ensure_dataset_exists ¶
_ensure_dataset_exists()
Ensure Opik dataset exists for tracking prompt versions.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
225 226 227 228 229 230 231 232 233 234 235 236 237 | |
_get_prompt_data ¶
_get_prompt_data()
Fetch latest prompt from Opik.
Returns:
-
tuple[Prompt | None, str | list[dict[str, str]] | None]–Tuple of (Prompt object or None, prompt data as str/list or None).
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
_sync_local_policy ¶
_sync_local_policy(local_data, opik_prompt)
Handle local sync policy - local file takes precedence.
Parameters:
-
local_data(str | list | None) –Local prompt data (string or list of messages) or None.
-
opik_prompt(Prompt | None) –Opik Prompt object or None.
Returns:
-
tuple[Prompt | None, str | list | None]–Tuple of (Prompt object, prompt data).
Raises:
-
DatasetError–If no prompt found locally or in Opik.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
405 406 407 408 409 410 411 412 413 414 415 416 417 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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
_sync_remote_policy ¶
_sync_remote_policy(local_data, opik_prompt)
Handle remote sync policy - Opik version takes precedence.
Parameters:
-
local_data(str | list | None) –Local prompt data (string or list of messages) or None.
-
opik_prompt(Prompt | None) –Opik Prompt object or None.
Returns:
-
tuple[Prompt | None, str | list | None]–Tuple of (Prompt object, prompt data).
Raises:
-
DatasetError–If no remote prompt exists in Opik.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
_sync_strict_policy ¶
_sync_strict_policy(local_data, opik_prompt)
Handle strict sync policy - error if local and remote differ.
Parameters:
-
local_data(str | list | None) –Local prompt data (string or list of messages) or None.
-
opik_prompt(Prompt | None) –Opik Prompt object or None.
Returns:
-
tuple[Prompt | None, str | list | None]–Tuple of (Prompt object, prompt data).
Raises:
-
DatasetError–If local and remote prompts don't match or if either is missing.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
_sync_with_opik ¶
_sync_with_opik(local_data, opik_prompt)
Synchronise local file and Opik prompt based on sync policy.
Parameters:
-
local_data(str | list | None) –Local prompt data (string or list of messages) or None.
-
opik_prompt(Prompt | None) –Opik Prompt object or None.
Returns:
-
tuple[Prompt | None, str | list | None]–Tuple of (Prompt object, prompt data) after synchronisation.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
_validate_init_params ¶
_validate_init_params(
filepath, prompt_type, sync_policy, mode
)
Validate initialisation parameters.
Parameters:
-
filepath(str) –File path to validate.
-
prompt_type(str) –Prompt type to validate.
-
sync_policy(str) –Sync policy to validate.
-
mode(str) –Mode to validate.
Raises:
-
DatasetError–If parameters are invalid.
-
ImportError–If required packages are missing.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
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 | |
load ¶
load()
Load prompt with synchronisation logic.
Returns:
-
ChatPromptTemplate–If mode="langchain", ready-to-use LangChain template.
-
Any–If mode="sdk", raw Opik prompt object.
Raises:
-
DatasetError–If prompt cannot be loaded or has invalid format.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
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 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | |
preview ¶
preview()
Generate a JSON-compatible preview for Kedro-Viz.
Returns:
-
JSONPreview–A Kedro-Viz-compatible preview of the prompt.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
572 573 574 575 576 577 578 | |
save ¶
save(data)
Save prompt to Opik.
Parameters:
-
data(str | list[dict[str, str]]) –The prompt content to save. Can be string for text prompts or list of message dictionaries for chat prompts.
Raises:
-
DatasetError–If data format is invalid or save fails.
Source code in kedro_datasets_experimental/opik/prompt_dataset.py
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 | |