Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not using Tools in Langchain_Ollama causes: 'NoneType' object is not iterable #28312

Open
5 tasks done
bauerem opened this issue Nov 23, 2024 · 7 comments
Open
5 tasks done
Labels
investigate Flagged for investigation.

Comments

@bauerem
Copy link

bauerem commented Nov 23, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_ollama import ChatOllama

llm = ChatOllama(
model="llama3.1",
temperature=0,
disable_streaming=True
)

messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
print(ai_msg)

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
File "/home/user1/Repo/ollama-cloud/use4.py", line 16, in
ai_msg = llm.invoke(messages)
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 286, in invoke
self.generate_prompt(
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 786, in generate_prompt
return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 643, in generate
raise e
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 633, in generate
self._generate_with_cache(
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 851, in _generate_with_cache
result = self._generate(
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_ollama/chat_models.py", line 648, in _generate
final_chunk = self._chat_stream_with_aggregation(
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_ollama/chat_models.py", line 560, in _chat_stream_with_aggregation
tool_calls=_get_tool_calls_from_response(stream_resp),
File "/home/user1/Repo/ollama-cloud/venv/lib/python3.10/site-packages/langchain_ollama/chat_models.py", line 71, in _get_tool_calls_from_response
for tc in response["message"]["tool_calls"]:
TypeError: 'NoneType' object is not iterable

Description

Trying to simple get a chat response from ChatOllama.

System Info

System Information

OS: Linux
OS Version: #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2
Python Version: 3.10.12 (main, Nov 6 2024, 20:22:13) [GCC 11.4.0]

Package Information

langchain_core: 0.3.20
langchain: 0.3.7
langchain_community: 0.3.7
langsmith: 0.1.144
langchain_ollama: 0.2.0
langchain_text_splitters: 0.3.2

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.7
async-timeout: 4.0.3
dataclasses-json: 0.6.7
httpx: 0.27.2
httpx-sse: 0.4.0
jsonpatch: 1.33
numpy: 1.26.4
ollama: 0.4.0
orjson: 3.10.11
packaging: 24.2
pydantic: 2.10.1
pydantic-settings: 2.6.1
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
SQLAlchemy: 2.0.35
tenacity: 9.0.0
typing-extensions: 4.12.2

@langcarl langcarl bot added the investigate Flagged for investigation. label Nov 23, 2024
@Fernando7181
Copy link

we had another issue that was similar to this one, maybe try python response["message"]["tool_calls"] is not None and see if that works

@bauerem
Copy link
Author

bauerem commented Nov 23, 2024

Yes, that is how I would also fix the bug in the langchain implementation

@Fernando7181
Copy link

Yes, that is how I would also fix the bug in the langchain implementation

did it work?

@keenborder786
Copy link
Contributor

keenborder786 commented Nov 23, 2024

@bauerem please upgrade to latest version of langchain-ollama. It should fix the issue.

@espositodaniele
Copy link

Same error here:

Package Information

ollama 0.4.1
langchain 0.3.8
langchain-community 0.3.8
langchain-core 0.3.21
langchain-ollama 0.2.0

Code example

from langchain_ollama import ChatOllama

model = ChatOllama(model="llama3.2", temperature=0)
model.invoke("Chi è il presidente degli Stati Uniti?")

Error

TypeError Traceback (most recent call last)
Cell In[34], line 4
1 from langchain_ollama import ChatOllama
3 model = ChatOllama(model="llama3.2", temperature=0)
----> 4 model.invoke("Chi è il presidente degli Stati Uniti?")

File ~/aidev/pdf-rag/.venv/lib/python3.13/site-packages/langchain_core/language_models/chat_models.py:286, in BaseChatModel.invoke(self, input, config, stop, **kwargs)
275 def invoke(
276 self,
277 input: LanguageModelInput,
(...)
281 **kwargs: Any,
282 ) -> BaseMessage:
283 config = ensure_config(config)
284 return cast(
285 ChatGeneration,
--> 286 self.generate_prompt(
287 [self._convert_input(input)],
288 stop=stop,
289 callbacks=config.get("callbacks"),
290 tags=config.get("tags"),
291 metadata=config.get("metadata"),
292 run_name=config.get("run_name"),
293 run_id=config.pop("run_id", None),
...
76 )
77 )
78 return tool_calls

TypeError: 'NoneType' object is not iterable

@nourishnew
Copy link

nourishnew commented Nov 25, 2024

@keenborder786 Latest version doesn't seem to have the None check.

@keenborder786
Copy link
Contributor

@nourishnew yeap you are correct I have fixed it it in the PR. Hopefully in the next release it will be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Flagged for investigation.
Projects
None yet
Development

No branches or pull requests

5 participants