fix(tracing): use Langfuse compile_prompt with {{variable}} syntax

- tracing.py: add compile_prompt() that uses Langfuse .compile(**vars)
  for {{variable}} substitution, falls back to Python .format() for
  hardcoded {variable} templates
- agent_runner.py: replace _get_system_prompt().format() with
  tracing.compile_prompt() for batch_file_classifier, batch_processing,
  batch_cloud_processing prompts
- journey.py: replace get_prompt + .format() with compile_prompt()
  for journey_system prompt
- chat tracing.py: add compile_prompt() for parity (chat prompts
  currently have no variables, but ready for future use)
- Remove unused _get_system_prompt helper
This commit is contained in:
Roberto Musso
2026-03-23 22:39:27 +01:00
parent 55500cc818
commit ccba54ac24
4 changed files with 120 additions and 36 deletions

View File

@@ -167,12 +167,6 @@ and what you created.
"""
def _get_system_prompt(langfuse_name: str, fallback: str) -> str:
"""Fetch a managed prompt from Langfuse, falling back to the hardcoded string."""
managed = tracing.get_prompt(langfuse_name, fallback=None)
return managed if managed is not None else fallback
# ── LLM tool-calling loop ─────────────────────────────────────────────────
@@ -427,9 +421,13 @@ async def _classify_file(
if d in _DOMAIN_DESCRIPTIONS
)
system = _get_system_prompt("batch_file_classifier", _STEP1_SYSTEM_PROMPT).format(
domain_definitions=domain_definitions,
projects_list=projects_list,
system = tracing.compile_prompt(
"batch_file_classifier",
fallback=_STEP1_SYSTEM_PROMPT,
variables={
"domain_definitions": domain_definitions,
"projects_list": projects_list,
},
)
llm = get_llm(callbacks=[langfuse_handler] if langfuse_handler else None)
@@ -604,13 +602,15 @@ async def run_local_agent(user_id: str, trigger_data: dict[str, Any], *, langfus
existing_context = "\n\n".join(existing_blocks)
system_prompt = _get_system_prompt(
"batch_processing", _PROCESSING_SYSTEM_PROMPT
).format(
existing_context=existing_context,
project_context=project_context,
data_types=", ".join(domains),
custom_prompt_section=custom_section,
system_prompt = tracing.compile_prompt(
"batch_processing",
fallback=_PROCESSING_SYSTEM_PROMPT,
variables={
"existing_context": existing_context,
"project_context": project_context,
"data_types": ", ".join(domains),
"custom_prompt_section": custom_section,
},
)
processing_tools = _build_processing_tools(domains)
@@ -790,13 +790,15 @@ async def run_cloud_agent(user_id: str, config_id: str, *, langfuse_handler: Any
continue
items_processed += 1
processing_prompt = _get_system_prompt(
"batch_cloud_processing", _CLOUD_PROCESSING_PROMPT
).format(
data_types=", ".join(config.data_types),
project_context="Determine the appropriate project from the message context.",
file_list=f"Message from {config.provider} (id: {msg.id})",
custom_prompt_section=custom_section,
processing_prompt = tracing.compile_prompt(
"batch_cloud_processing",
fallback=_CLOUD_PROCESSING_PROMPT,
variables={
"data_types": ", ".join(config.data_types),
"project_context": "Determine the appropriate project from the message context.",
"file_list": f"Message from {config.provider} (id: {msg.id})",
"custom_prompt_section": custom_section,
},
)
try: