Skip to content

Commit f432610

Browse files
authored
Python: Display system prompt in langfuse (#2653)
* Display system prompt in langfuse * Explain that the system_instructions are put first into the list
1 parent 900c2a0 commit f432610

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

python/packages/core/agent_framework/_types.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,20 +2125,31 @@ def text(self) -> str:
21252125
return " ".join(content.text for content in self.contents if isinstance(content, TextContent))
21262126

21272127

2128-
def prepare_messages(messages: str | ChatMessage | list[str] | list[ChatMessage]) -> list[ChatMessage]:
2128+
def prepare_messages(
2129+
messages: str | ChatMessage | list[str] | list[ChatMessage], system_instructions: str | list[str] | None = None
2130+
) -> list[ChatMessage]:
21292131
"""Convert various message input formats into a list of ChatMessage objects.
21302132
21312133
Args:
21322134
messages: The input messages in various supported formats.
2135+
system_instructions: The system instructions. They will be inserted to the start of the messages list.
21332136
21342137
Returns:
21352138
A list of ChatMessage objects.
21362139
"""
2140+
if system_instructions is not None:
2141+
if isinstance(system_instructions, str):
2142+
system_instructions = [system_instructions]
2143+
system_instruction_messages = [ChatMessage(role="system", text=instr) for instr in system_instructions]
2144+
else:
2145+
system_instruction_messages = []
2146+
21372147
if isinstance(messages, str):
2138-
return [ChatMessage(role="user", text=messages)]
2148+
return [*system_instruction_messages, ChatMessage(role="user", text=messages)]
21392149
if isinstance(messages, ChatMessage):
2140-
return [messages]
2141-
return_messages: list[ChatMessage] = []
2150+
return [*system_instruction_messages, messages]
2151+
2152+
return_messages: list[ChatMessage] = system_instruction_messages
21422153
for msg in messages:
21432154
if isinstance(msg, str):
21442155
msg = ChatMessage(role="user", text=msg)

python/packages/core/agent_framework/observability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ def _capture_messages(
14221422
"""Log messages with extra information."""
14231423
from ._types import prepare_messages
14241424

1425-
prepped = prepare_messages(messages)
1425+
prepped = prepare_messages(messages, system_instructions=system_instructions)
14261426
otel_messages: list[dict[str, Any]] = []
14271427
for index, message in enumerate(prepped):
14281428
otel_messages.append(_to_otel_message(message))

0 commit comments

Comments
 (0)