From c082ccac823bd13315074307c854647c02d5c1e2 Mon Sep 17 00:00:00 2001 From: Pierre-Jean Morel Date: Wed, 27 May 2026 17:56:34 +0200 Subject: [PATCH] feat: add config into YAML frontmatter for prompt export --- pyproject.toml | 1 + .../langfuse/prompt_exporter.py | 37 ++++++++----------- uv.lock | 20 ++++++++++ 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 338f3b9..149b942 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ dependencies = [ "typer>=0.24.1", "langfuse>=3.14.5", "ruff>=0.15.5", + "pyyaml>=6.0.3", ] [dependency-groups] diff --git a/src/bto_langfuse_cli/langfuse/prompt_exporter.py b/src/bto_langfuse_cli/langfuse/prompt_exporter.py index 3499213..f4d5189 100644 --- a/src/bto_langfuse_cli/langfuse/prompt_exporter.py +++ b/src/bto_langfuse_cli/langfuse/prompt_exporter.py @@ -1,6 +1,8 @@ from abc import ABC, abstractmethod from datetime import datetime from pathlib import Path +from typing import Any +import yaml from langfuse.api import PromptType from langfuse.api.prompts.types.prompt_meta import PromptMeta @@ -37,22 +39,6 @@ class MarkdownPromptExporter(PromptExporter): and XML section for Chat prompts. """ - def _yaml_quote(self, s: str) -> str: - if s == "": - return '""' - if any(c in s for c in (":", "#", "\n", '"')) or s.strip() != s: - escaped = s.replace("\\", "\\\\").replace('"', '\\"') - return f'"{escaped}"' - return s - - def _format_yaml_block_list(self, key: str, items: list[str]) -> str: - if not items: - return f"{key}: []" - lines = [f"{key}:"] - for item in items: - lines.append(f" - {self._yaml_quote(item)}") - return "\n".join(lines) - def _metadata_header( self, name: str, @@ -60,16 +46,22 @@ def _metadata_header( prompt_type: str, labels: list[str], tags: list[str], + config: dict[str, Any], last_updated_at: datetime, ) -> str: lines = [ "---", - f"name: {self._yaml_quote(name)}", - f"version: {version}", - f"type: {prompt_type}", - self._format_yaml_block_list("labels", labels), - self._format_yaml_block_list("tags", tags), - f"last_updated_at: {self._yaml_quote(last_updated_at.isoformat())}", + yaml.dump( + { + "name": name, + "version": version, + "type": prompt_type, + "labels": labels, + "tags": tags, + "config": config, + "last_updated_at": last_updated_at.isoformat(), + } + ), "---", ] return "\n".join(lines) + "\n" @@ -105,6 +97,7 @@ def export( prompt_type=langfuse_prompt_type, labels=list(prompt_client.labels), tags=list(prompt_client.tags), + config=prompt_client.config, last_updated_at=meta.last_updated_at, ) diff --git a/uv.lock b/uv.lock index 96d5715..50d0498 100644 --- a/uv.lock +++ b/uv.lock @@ -47,6 +47,7 @@ version = "1.0.3" source = { editable = "." } dependencies = [ { name = "langfuse" }, + { name = "pyyaml" }, { name = "ruff" }, { name = "typer" }, ] @@ -59,6 +60,7 @@ dev = [ [package.metadata] requires-dist = [ { name = "langfuse", specifier = ">=3.14.5" }, + { name = "pyyaml", specifier = ">=6.0.3" }, { name = "ruff", specifier = ">=0.15.5" }, { name = "typer", specifier = ">=0.24.1" }, ] @@ -441,6 +443,24 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/c7/21/705964c7812476f378728bdf590ca4b771ec72385c533964653c68e86bdc/pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b", size = 1225217, upload-time = "2025-06-21T13:39:07.939Z" }, ] +[[package]] +name = "pyyaml" +version = "6.0.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/05/8e/961c0007c59b8dd7729d542c61a4d537767a59645b82a0b521206e1e25c2/pyyaml-6.0.3.tar.gz", hash = "sha256:d76623373421df22fb4cf8817020cbb7ef15c725b9d5e45f17e189bfc384190f", size = 130960, upload-time = "2025-09-25T21:33:16.546Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d1/11/0fd08f8192109f7169db964b5707a2f1e8b745d4e239b784a5a1dd80d1db/pyyaml-6.0.3-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:8da9669d359f02c0b91ccc01cac4a67f16afec0dac22c2ad09f46bee0697eba8", size = 181669, upload-time = "2025-09-25T21:32:23.673Z" }, + { url = "https://files.pythonhosted.org/packages/b1/16/95309993f1d3748cd644e02e38b75d50cbc0d9561d21f390a76242ce073f/pyyaml-6.0.3-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:2283a07e2c21a2aa78d9c4442724ec1eb15f5e42a723b99cb3d822d48f5f7ad1", size = 173252, upload-time = "2025-09-25T21:32:25.149Z" }, + { url = "https://files.pythonhosted.org/packages/50/31/b20f376d3f810b9b2371e72ef5adb33879b25edb7a6d072cb7ca0c486398/pyyaml-6.0.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ee2922902c45ae8ccada2c5b501ab86c36525b883eff4255313a253a3160861c", size = 767081, upload-time = "2025-09-25T21:32:26.575Z" }, + { url = "https://files.pythonhosted.org/packages/49/1e/a55ca81e949270d5d4432fbbd19dfea5321eda7c41a849d443dc92fd1ff7/pyyaml-6.0.3-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a33284e20b78bd4a18c8c2282d549d10bc8408a2a7ff57653c0cf0b9be0afce5", size = 841159, upload-time = "2025-09-25T21:32:27.727Z" }, + { url = "https://files.pythonhosted.org/packages/74/27/e5b8f34d02d9995b80abcef563ea1f8b56d20134d8f4e5e81733b1feceb2/pyyaml-6.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0f29edc409a6392443abf94b9cf89ce99889a1dd5376d94316ae5145dfedd5d6", size = 801626, upload-time = "2025-09-25T21:32:28.878Z" }, + { url = "https://files.pythonhosted.org/packages/f9/11/ba845c23988798f40e52ba45f34849aa8a1f2d4af4b798588010792ebad6/pyyaml-6.0.3-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:f7057c9a337546edc7973c0d3ba84ddcdf0daa14533c2065749c9075001090e6", size = 753613, upload-time = "2025-09-25T21:32:30.178Z" }, + { url = "https://files.pythonhosted.org/packages/3d/e0/7966e1a7bfc0a45bf0a7fb6b98ea03fc9b8d84fa7f2229e9659680b69ee3/pyyaml-6.0.3-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:eda16858a3cab07b80edaf74336ece1f986ba330fdb8ee0d6c0d68fe82bc96be", size = 794115, upload-time = "2025-09-25T21:32:31.353Z" }, + { url = "https://files.pythonhosted.org/packages/de/94/980b50a6531b3019e45ddeada0626d45fa85cbe22300844a7983285bed3b/pyyaml-6.0.3-cp313-cp313-win32.whl", hash = "sha256:d0eae10f8159e8fdad514efdc92d74fd8d682c933a6dd088030f3834bc8e6b26", size = 137427, upload-time = "2025-09-25T21:32:32.58Z" }, + { url = "https://files.pythonhosted.org/packages/97/c9/39d5b874e8b28845e4ec2202b5da735d0199dbe5b8fb85f91398814a9a46/pyyaml-6.0.3-cp313-cp313-win_amd64.whl", hash = "sha256:79005a0d97d5ddabfeeea4cf676af11e647e41d81c9a7722a193022accdb6b7c", size = 154090, upload-time = "2025-09-25T21:32:33.659Z" }, + { url = "https://files.pythonhosted.org/packages/73/e8/2bdf3ca2090f68bb3d75b44da7bbc71843b19c9f2b9cb9b0f4ab7a5a4329/pyyaml-6.0.3-cp313-cp313-win_arm64.whl", hash = "sha256:5498cd1645aa724a7c71c8f378eb29ebe23da2fc0d7a08071d89469bf1d2defb", size = 140246, upload-time = "2025-09-25T21:32:34.663Z" }, +] + [[package]] name = "requests" version = "2.32.5"