Skip to content

Conversation

@nimarb
Copy link
Contributor

@nimarb nimarb commented Sep 17, 2025

Important

Fixes bug in ChatPromptClient.compile() to preserve tool calls in message placeholders, with a new test added for verification.

  • Behavior:
    • Fixes bug in ChatPromptClient.compile() to preserve tool calls in message placeholders.
    • Ensures all fields from original messages are retained, including role and content.
  • Tests:
    • Adds test_tool_calls_preservation_in_message_placeholder() in test_prompt_compilation.py to verify tool calls are preserved during compilation.

This description was created by Ellipsis for ab40d06. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Summary

Updated On: 2025-09-17 12:24:14 UTC

This PR fixes a bug in the Langfuse prompt system where message placeholders were losing important OpenAI message fields like tool_calls during compilation. The change affects the prompt compilation logic in langfuse/model.py and adds a corresponding test in tests/test_prompt_compilation.py.

The Core Issue: The previous implementation of placeholder message compilation only preserved role and content fields by creating a restrictive ChatMessageDict structure. This caused data loss for complex chat messages that contain additional metadata like tool_calls, function_call, or other OpenAI-specific properties.

The Solution: The fix modifies the compilation logic to preserve all fields from the original message dictionary while ensuring backward compatibility. Instead of creating a new ChatMessageDict with only role and content, the code now:

  1. Creates a copy of the entire original message dictionary
  2. Ensures role and content fields are always present with sensible defaults
  3. Compiles template variables within the content field
  4. Preserves all other fields unchanged

Integration with Codebase: This change fits into Langfuse's prompt management system, which supports message placeholders for conversation history injection. The BasePromptClient and ChatPromptClient classes are part of the framework's template compilation pipeline, where prompts can contain variable placeholders ({{variable_name}}) that get resolved at runtime. The fix ensures that when message objects are used as placeholder values, their complete structure is maintained.

Type System Updates: The return type annotations for the compile method in both abstract and concrete classes were updated to include Dict[str, Any] alongside existing types, reflecting that compiled messages can now contain arbitrary fields beyond the basic ChatMessageDict structure.

Confidence score: 4/5

  • This PR addresses a clear bug with a targeted fix that preserves backward compatibility
  • The solution follows the simpler approach principle by extending existing logic rather than adding complex state tracking
  • Comprehensive test coverage ensures the fix works correctly and prevents regression
  • Pay close attention to the type annotation changes and ensure downstream consumers can handle the expanded return types

Context used:

Rule - Prefer simpler solutions when fixing bugs - if the root cause can be addressed by extending an existing condition rather than adding complex state tracking, choose the simpler approach. (link)

@nimarb nimarb marked this pull request as ready for review September 17, 2025 12:23
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@nimarb nimarb requested a review from hassiebp September 17, 2025 12:55
@nimarb
Copy link
Contributor Author

nimarb commented Sep 17, 2025

should fix: langfuse/langfuse#9051

@nimarb nimarb merged commit ce29143 into main Sep 17, 2025
11 checks passed
@nimarb nimarb deleted the nimar/lfe-6649-messageplaceholder-retains-tool-calls branch September 17, 2025 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants