Skip to content

Commit d867dce

Browse files
jsonbaileyclaude
andcommitted
fix: seed history with config messages at init instead of prepending per call
Config messages (system prompt, instructions) are added once when the runner is constructed, not re-injected on every run() call. OpenAI collapses _config_messages into _history at init; LangChain seeds InMemoryChatMessageHistory with the converted messages so they appear naturally at the start of the thread. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent fb7cd2e commit d867dce

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

packages/ai-providers/server-ai-langchain/src/ldai_langchain/langchain_model_runner.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ class LangChainModelRunner(Runner):
2727

2828
def __init__(self, llm: BaseChatModel, config_messages: Optional[List[LDMessage]] = None):
2929
self._llm = llm
30-
self._config_messages: List[LDMessage] = list(config_messages or [])
31-
self._chat_history = InMemoryChatMessageHistory()
30+
self._chat_history = InMemoryChatMessageHistory(
31+
messages=convert_messages_to_langchain(config_messages or [])
32+
)
3233

3334
def get_llm(self) -> BaseChatModel:
3435
"""
@@ -46,10 +47,10 @@ async def run(
4647
"""
4748
Run the LangChain model with the given input.
4849
49-
Prepends config messages and accumulated conversation history (stored as
50-
native LangChain messages via InMemoryChatMessageHistory) before the user
51-
message. On success, appends the exchange to chat history so subsequent
52-
calls include prior context.
50+
Sends the full chat history (seeded with config messages at construction
51+
time via InMemoryChatMessageHistory) plus the new user message. On
52+
success, appends the user/assistant exchange so subsequent calls include
53+
prior context.
5354
5455
:param input: A string prompt
5556
:param output_type: Optional JSON schema dict requesting structured output.
@@ -58,11 +59,7 @@ async def run(
5859
:return: :class:`RunnerResult` containing ``content``, ``metrics``,
5960
``raw`` and (when ``output_type`` is set) ``parsed``.
6061
"""
61-
langchain_messages = (
62-
convert_messages_to_langchain(self._config_messages)
63-
+ self._chat_history.messages
64-
+ [HumanMessage(content=input)]
65-
)
62+
langchain_messages = self._chat_history.messages + [HumanMessage(content=input)]
6663

6764
if output_type is not None:
6865
result = await self._run_structured(langchain_messages, output_type)

packages/ai-providers/server-ai-openai/src/ldai_openai/openai_model_runner.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ def __init__(
3333
self._client = client
3434
self._model_name = model_name
3535
self._parameters = parameters
36-
self._config_messages: List[LDMessage] = list(config_messages or [])
37-
self._history: List[LDMessage] = []
36+
self._history: List[LDMessage] = list(config_messages or [])
3837

3938
async def run(
4039
self,
@@ -44,9 +43,9 @@ async def run(
4443
"""
4544
Run the OpenAI model with the given input.
4645
47-
Prepends config messages and accumulated conversation history before the
48-
user message. On success, appends the user/assistant exchange to history
49-
so subsequent calls include prior context.
46+
Sends the full conversation history (seeded with config messages at
47+
construction time) plus the new user message. On success, appends the
48+
user/assistant exchange to history so subsequent calls include prior context.
5049
5150
:param input: A string prompt
5251
:param output_type: Optional JSON schema dict requesting structured output.
@@ -56,7 +55,7 @@ async def run(
5655
``raw`` and (when ``output_type`` is set) ``parsed``.
5756
"""
5857
user_message = LDMessage(role='user', content=input)
59-
messages = self._config_messages + self._history + [user_message]
58+
messages = self._history + [user_message]
6059

6160
if output_type is not None:
6261
result = await self._run_structured(messages, output_type)

0 commit comments

Comments
 (0)