Python: Display system prompt in langfuse (#2653)

* Display system prompt in langfuse

* Explain that the system_instructions are put first into the list
This commit is contained in:
SuperKenVery
2025-12-06 01:11:29 +08:00
committed by GitHub
Unverified
parent 900c2a04fb
commit f43261094f
2 changed files with 16 additions and 5 deletions
+15 -4
View File
@@ -2125,20 +2125,31 @@ class ChatMessage(SerializationMixin):
return " ".join(content.text for content in self.contents if isinstance(content, TextContent))
def prepare_messages(messages: str | ChatMessage | list[str] | list[ChatMessage]) -> list[ChatMessage]:
def prepare_messages(
messages: str | ChatMessage | list[str] | list[ChatMessage], system_instructions: str | list[str] | None = None
) -> list[ChatMessage]:
"""Convert various message input formats into a list of ChatMessage objects.
Args:
messages: The input messages in various supported formats.
system_instructions: The system instructions. They will be inserted to the start of the messages list.
Returns:
A list of ChatMessage objects.
"""
if system_instructions is not None:
if isinstance(system_instructions, str):
system_instructions = [system_instructions]
system_instruction_messages = [ChatMessage(role="system", text=instr) for instr in system_instructions]
else:
system_instruction_messages = []
if isinstance(messages, str):
return [ChatMessage(role="user", text=messages)]
return [*system_instruction_messages, ChatMessage(role="user", text=messages)]
if isinstance(messages, ChatMessage):
return [messages]
return_messages: list[ChatMessage] = []
return [*system_instruction_messages, messages]
return_messages: list[ChatMessage] = system_instruction_messages
for msg in messages:
if isinstance(msg, str):
msg = ChatMessage(role="user", text=msg)
@@ -1422,7 +1422,7 @@ def _capture_messages(
"""Log messages with extra information."""
from ._types import prepare_messages
prepped = prepare_messages(messages)
prepped = prepare_messages(messages, system_instructions=system_instructions)
otel_messages: list[dict[str, Any]] = []
for index, message in enumerate(prepped):
otel_messages.append(_to_otel_message(message))