ollama - 💡(How to fix) Fix LangChain: chatollama invocation fails when messages contain only SystemMessage/AIMessage without any HumanMessage [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#15507Fetched 2026-04-12 13:24:13
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
labeled ×1

Error Message

2026-04-11T20:20:44.443753Z [info ] Retrying task llm_with_tools after 1.15 seconds (attempt 1) after ResponseError Service Temporarily Unavailable (status code: 503) [langgraph.pregel._retry] api_variant=local_dev assistant_id=fe096781-5601-53d2-b2f6-0d3403f7e9ca graph_id=agent langgraph_api_version=0.7.56 request_id=bd85cd81-e24d-4e35-adf2-a9eecd822482 run_attempt=1 run_id=019d7e32-29ee-7103-9a76-8df5d8300795 thread_id=2a49f20a-8cf8-4530-aa65-232198cd0833 thread_name=MainThread Traceback (most recent call last): File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph\pregel_retry.py", line 211, in arun_with_retry return await task.proc.ainvoke(task.input, config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph_internal_runnable.py", line 705, in ainvoke input = await asyncio.create_task( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph_internal_runnable.py", line 473, in ainvoke ret = await self.afunc(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\AI\basic-agent\basic_deep_agent\src\AI_Nodes\nodes.py", line 93, in llm_with_tools response = await llm_tools.ainvoke(messags_after_summarization) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\runnables\base.py", line 5708, in ainvoke return await self.bound.ainvoke( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 425, in ainvoke llm_result = await self.agenerate_prompt( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1134, in agenerate_prompt return await self.agenerate( ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1092, in agenerate raise exceptions[0] File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1318, in _agenerate_with_cache async for chunk in self._astream(messages, stop=stop, **kwargs): File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 1193, in _astream async for chunk in self._aiterate_over_stream(messages, stop, **kwargs): File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 1131, in _aiterate_over_stream async for stream_resp in self._acreate_chat_stream(messages, stop, **kwargs): File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 937, in _acreate_chat_stream async for part in await self._async_client.chat(**chat_params): File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\ollama_client.py", line 741, in inner raise ResponseError(e.response.text, e.response.status_code) from None ollama._types.ResponseError: Service Temporarily Unavailable (status code: 503) During task with name 'llm_with_tools' and id 'fa2904e7-60e4-5492-37ff-aec55365e083' sleeping for 60 seconds before retrying.

Fix Action

Fix / Workaround

Current Workaround: Insert a dummy HumanMessage before the system message when this scenario occurs

Code Example

2026-04-11T20:20:44.443753Z [info     ] Retrying task llm_with_tools after 1.15 seconds (attempt 1) after ResponseError Service Temporarily Unavailable (status code: 503) [langgraph.pregel._retry] api_variant=local_dev assistant_id=fe096781-5601-53d2-b2f6-0d3403f7e9ca graph_id=agent langgraph_api_version=0.7.56 request_id=bd85cd81-e24d-4e35-adf2-a9eecd822482 run_attempt=1 run_id=019d7e32-29ee-7103-9a76-8df5d8300795 thread_id=2a49f20a-8cf8-4530-aa65-232198cd0833 thread_name=MainThread
Traceback (most recent call last):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph\pregel\_retry.py", line 211, in arun_with_retry
    return await task.proc.ainvoke(task.input, config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph\_internal\_runnable.py", line 705, in ainvoke
    input = await asyncio.create_task(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph\_internal\_runnable.py", line 473, in ainvoke
    ret = await self.afunc(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\AI\basic-agent\basic_deep_agent\src\AI_Nodes\nodes.py", line 93, in llm_with_tools
    response = await llm_tools.ainvoke(messags_after_summarization)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\runnables\base.py", line 5708, in ainvoke
    return await self.bound.ainvoke(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 425, in ainvoke
    llm_result = await self.agenerate_prompt(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1134, in agenerate_prompt
    return await self.agenerate(
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1092, in agenerate
    raise exceptions[0]
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1318, in _agenerate_with_cache
    async for chunk in self._astream(messages, stop=stop, **kwargs):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 1193, in _astream
    async for chunk in self._aiterate_over_stream(messages, stop, **kwargs):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 1131, in _aiterate_over_stream
    async for stream_resp in self._acreate_chat_stream(messages, stop, **kwargs):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 937, in _acreate_chat_stream
    async for part in await self._async_client.chat(**chat_params):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\ollama\_client.py", line 741, in inner
    raise ResponseError(e.response.text, e.response.status_code) from None
ollama._types.ResponseError: Service Temporarily Unavailable (status code: 503)
During task with name 'llm_with_tools' and id 'fa2904e7-60e4-5492-37ff-aec55365e083'
sleeping for 60 seconds before retrying.
RAW_BUFFERClick to expand / collapse

What is the issue?

When invoking the Ollama LLM via ChatOllama with a message list that contains only SystemMessage(s)/AIMessage(s) and no HumanMessage, the invocation fails with an error. This is problematic when using message summarization libraries like langmem which can sometimes replace all messages with a single SystemMessage during the summarization process.

Steps to Reproduce:

  1. Create a ChatOllama model instance
  2. Prepare a message list containing only SystemMessage(s) - for example, after a summarization process collapses all previous messages into a single system message
  3. Attempt to invoke the model with: model.ainvoke(messages)

Model being used: minimax-m2.7:cloud (ollama pro)

Code I have:

`async def llm_with_tools(state: State, runtime: Runtime):

"""
Processes messages using LLM with tools when required.

Args:
    state: Current state containing messages and tool requirement
    
Returns:
    Dict containing updated messages
"""
info = runtime.execution_info
if info.node_attempt > 1:
    print("sleeping for 60 seconds before retrying.")
    await asyncio.sleep(60)     
# Create the prompt template with system prompt and messages
agent_description = AGENT_DESCRIPTION
prompt = PromptTemplate.from_template(agent_description)
system_message = [SystemMessage(content=prompt.format(current_date=state["current_date"], skills_description=state["skills_description"]))]

#add systemmessage to the beginning of the messages to be summarized so that it is included in the summary.
state["messages"] = system_message + state["messages"]

summarization_result = await asummarize_messages(
    state["messages"],
    running_summary=state.get("summary"),
    model=llm,
    max_tokens=10000,
    max_tokens_before_summary=5000,
    max_summary_tokens=1000,
    initial_summary_prompt=DEFAULT_INITIAL_SUMMARY_PROMPT,
    existing_summary_prompt=DEFAULT_EXISTING_SUMMARY_PROMPT,
)
messags_after_summarization = summarization_result.messages

# If there are exactly two system messages after summarization, convert the second one to an AIMessage
system_message_indices = [i for i, msg in enumerate(messags_after_summarization) if isinstance(msg, SystemMessage)]
if len(system_message_indices) == 2:
    print(system_message_indices)
    second_system_idx = system_message_indices[1]
    messags_after_summarization[second_system_idx] = AIMessage(content=messags_after_summarization[second_system_idx].content)
    # Insert a HumanMessage after the second system message using the first message from state
    # human_msg = HumanMessage(content="Look at the summary and the conversation below and decide what to do next.")
    # messags_after_summarization.insert(second_system_idx, human_msg)
    print(messags_after_summarization)
await asyncio.sleep(10)
response = await llm_tools.ainvoke(messags_after_summarization)
state_update = {"messages": [response]}
if summarization_result.running_summary:
    state_update["summary"] = summarization_result.running_summary

# Return the response as a message
return state_update`

Full Code: https://github.com/Bkbest/basic_deep_agent

Expected Behavior:

The model should be able to process message list even if it does not contain a HumanMessage.

Current Workaround: Insert a dummy HumanMessage before the system message when this scenario occurs

Versions I am using:

Name: langchain-ollama Version: 1.0.1

Name: langchain Version: 1.2.10

Name: langmem Version: 0.0.30

Relevant log output

2026-04-11T20:20:44.443753Z [info     ] Retrying task llm_with_tools after 1.15 seconds (attempt 1) after ResponseError Service Temporarily Unavailable (status code: 503) [langgraph.pregel._retry] api_variant=local_dev assistant_id=fe096781-5601-53d2-b2f6-0d3403f7e9ca graph_id=agent langgraph_api_version=0.7.56 request_id=bd85cd81-e24d-4e35-adf2-a9eecd822482 run_attempt=1 run_id=019d7e32-29ee-7103-9a76-8df5d8300795 thread_id=2a49f20a-8cf8-4530-aa65-232198cd0833 thread_name=MainThread
Traceback (most recent call last):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph\pregel\_retry.py", line 211, in arun_with_retry
    return await task.proc.ainvoke(task.input, config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph\_internal\_runnable.py", line 705, in ainvoke
    input = await asyncio.create_task(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langgraph\_internal\_runnable.py", line 473, in ainvoke
    ret = await self.afunc(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\AI\basic-agent\basic_deep_agent\src\AI_Nodes\nodes.py", line 93, in llm_with_tools
    response = await llm_tools.ainvoke(messags_after_summarization)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\runnables\base.py", line 5708, in ainvoke
    return await self.bound.ainvoke(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 425, in ainvoke
    llm_result = await self.agenerate_prompt(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1134, in agenerate_prompt
    return await self.agenerate(
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1092, in agenerate
    raise exceptions[0]
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1318, in _agenerate_with_cache
    async for chunk in self._astream(messages, stop=stop, **kwargs):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 1193, in _astream
    async for chunk in self._aiterate_over_stream(messages, stop, **kwargs):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 1131, in _aiterate_over_stream
    async for stream_resp in self._acreate_chat_stream(messages, stop, **kwargs):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_ollama\chat_models.py", line 937, in _acreate_chat_stream
    async for part in await self._async_client.chat(**chat_params):
  File "C:\Users\aravi\AppData\Local\Programs\Python\Python312\Lib\site-packages\ollama\_client.py", line 741, in inner
    raise ResponseError(e.response.text, e.response.status_code) from None
ollama._types.ResponseError: Service Temporarily Unavailable (status code: 503)
During task with name 'llm_with_tools' and id 'fa2904e7-60e4-5492-37ff-aec55365e083'
sleeping for 60 seconds before retrying.

OS

Windows

GPU

Nvidia

CPU

AMD

Ollama version

0.17.1

extent analysis

TL;DR

The issue can be resolved by ensuring that the message list passed to the Ollama LLM via ChatOllama contains at least one HumanMessage, potentially by inserting a dummy HumanMessage when the list only contains SystemMessage(s) or AIMessage(s).

Guidance

  • Verify that the messags_after_summarization list contains at least one HumanMessage before invoking the Ollama LLM.
  • If the list only contains SystemMessage(s) or AIMessage(s), consider inserting a dummy HumanMessage to meet the LLM's requirements.
  • Review the asummarize_messages function to ensure it does not inadvertently remove HumanMessage(s) from the message list.
  • Check the Ollama LLM documentation to confirm its requirements for message lists and potential workarounds for this issue.

Example

# Insert a dummy HumanMessage if the list only contains SystemMessage(s) or AIMessage(s)
if not any(isinstance(msg, HumanMessage) for msg in messags_after_summarization):
    dummy_human_msg = HumanMessage(content="Dummy human message")
    messags_after_summarization.insert(0, dummy_human_msg)

Notes

The provided code and log output suggest that the Ollama LLM is failing when the message list only contains SystemMessage(s) or AIMessage(s). However, without further information about the LLM's requirements or the asummarize_messages function, it is difficult to provide a more specific solution.

Recommendation

Apply the workaround of inserting a dummy HumanMessage when the message list only contains SystemMessage(s) or AIMessage(s), as this has been reported to resolve the issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING