A quick LangChain and Ollama demo.

!pip install -q langchain langchain-ollama
from langchain_ollama import OllamaLLM
from IPython.display import display, Markdown

llm = OllamaLLM(model="gemma3:4b", temperature="0")

response = llm.invoke("What is the airspeed velocity of an unladen swallow?")
display(Markdown(response))
---------------------------------------------------------------------------
ConnectError                              Traceback (most recent call last)
File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_transports/default.py:101, in map_httpcore_exceptions()
    100 try:
--> 101     yield
    102 except Exception as exc:

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_transports/default.py:250, in HTTPTransport.handle_request(self, request)
    249 with map_httpcore_exceptions():
--> 250     resp = self._pool.handle_request(req)
    252 assert isinstance(resp.stream, typing.Iterable)

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:256, in ConnectionPool.handle_request(self, request)
    255     self._close_connections(closing)
--> 256     raise exc from None
    258 # Return the response. Note that in this case we still have to manage
    259 # the point at which the response is closed.

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:236, in ConnectionPool.handle_request(self, request)
    234 try:
    235     # Send the request on the assigned connection.
--> 236     response = connection.handle_request(
    237         pool_request.request
    238     )
    239 except ConnectionNotAvailable:
    240     # In some cases a connection may initially be available to
    241     # handle a request, but then become unavailable.
    242     #
    243     # In this case we clear the connection and try again.

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpcore/_sync/connection.py:101, in HTTPConnection.handle_request(self, request)
    100     self._connect_failed = True
--> 101     raise exc
    103 return self._connection.handle_request(request)

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpcore/_sync/connection.py:78, in HTTPConnection.handle_request(self, request)
     77 if self._connection is None:
---> 78     stream = self._connect(request)
     80     ssl_object = stream.get_extra_info("ssl_object")

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpcore/_sync/connection.py:124, in HTTPConnection._connect(self, request)
    123 with Trace("connect_tcp", logger, request, kwargs) as trace:
--> 124     stream = self._network_backend.connect_tcp(**kwargs)
    125     trace.return_value = stream

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpcore/_backends/sync.py:207, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options)
    202 exc_map: ExceptionMapping = {
    203     socket.timeout: ConnectTimeout,
    204     OSError: ConnectError,
    205 }
--> 207 with map_exceptions(exc_map):
    208     sock = socket.create_connection(
    209         address,
    210         timeout,
    211         source_address=source_address,
    212     )

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    157 try:
--> 158     self.gen.throw(value)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpcore/_exceptions.py:14, in map_exceptions(map)
     13     if isinstance(exc, from_exc):
---> 14         raise to_exc(exc) from exc
     15 raise

ConnectError: [Errno 61] Connection refused

The above exception was the direct cause of the following exception:

ConnectError                              Traceback (most recent call last)
Cell In[2], line 6
      2 from IPython.display import display, Markdown
      4 llm = OllamaLLM(model="gemma3:4b", temperature="0")
----> 6 response = llm.invoke("What is the airspeed velocity of an unladen swallow?")
      7 display(Markdown(response))

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/langchain_core/language_models/llms.py:373, in BaseLLM.invoke(self, input, config, stop, **kwargs)
    362 @override
    363 def invoke(
    364     self,
   (...)    369     **kwargs: Any,
    370 ) -> str:
    371     config = ensure_config(config)
    372     return (
--> 373         self.generate_prompt(
    374             [self._convert_input(input)],
    375             stop=stop,
    376             callbacks=config.get("callbacks"),
    377             tags=config.get("tags"),
    378             metadata=config.get("metadata"),
    379             run_name=config.get("run_name"),
    380             run_id=config.pop("run_id", None),
    381             **kwargs,
    382         )
    383         .generations[0][0]
    384         .text
    385     )

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/langchain_core/language_models/llms.py:784, in BaseLLM.generate_prompt(self, prompts, stop, callbacks, **kwargs)
    775 @override
    776 def generate_prompt(
    777     self,
   (...)    781     **kwargs: Any,
    782 ) -> LLMResult:
    783     prompt_strings = [p.to_string() for p in prompts]
--> 784     return self.generate(prompt_strings, stop=stop, callbacks=callbacks, **kwargs)

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/langchain_core/language_models/llms.py:1006, in BaseLLM.generate(self, prompts, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)
    987 if (self.cache is None and get_llm_cache() is None) or self.cache is False:
    988     run_managers = [
    989         callback_manager.on_llm_start(
    990             self._serialized,
   (...)   1004         )
   1005     ]
-> 1006     return self._generate_helper(
   1007         prompts,
   1008         stop,
   1009         run_managers,
   1010         new_arg_supported=bool(new_arg_supported),
   1011         **kwargs,
   1012     )
   1013 if len(missing_prompts) > 0:
   1014     run_managers = [
   1015         callback_managers[idx].on_llm_start(
   1016             self._serialized,
   (...)   1023         for idx in missing_prompt_idxs
   1024     ]

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/langchain_core/language_models/llms.py:810, in BaseLLM._generate_helper(self, prompts, stop, run_managers, new_arg_supported, **kwargs)
    799 def _generate_helper(
    800     self,
    801     prompts: list[str],
   (...)    806     **kwargs: Any,
    807 ) -> LLMResult:
    808     try:
    809         output = (
--> 810             self._generate(
    811                 prompts,
    812                 stop=stop,
    813                 # TODO: support multiple run managers
    814                 run_manager=run_managers[0] if run_managers else None,
    815                 **kwargs,
    816             )
    817             if new_arg_supported
    818             else self._generate(prompts, stop=stop)
    819         )
    820     except BaseException as e:
    821         for run_manager in run_managers:

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/langchain_ollama/llms.py:456, in OllamaLLM._generate(self, prompts, stop, run_manager, **kwargs)
    454 generations = []
    455 for prompt in prompts:
--> 456     final_chunk = self._stream_with_aggregation(
    457         prompt,
    458         stop=stop,
    459         run_manager=run_manager,
    460         verbose=self.verbose,
    461         **kwargs,
    462     )
    463     generations.append([final_chunk])
    464 return LLMResult(generations=generations)

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/langchain_ollama/llms.py:415, in OllamaLLM._stream_with_aggregation(self, prompt, stop, run_manager, verbose, **kwargs)
    413 final_chunk = None
    414 thinking_content = ""
--> 415 for stream_resp in self._create_generate_stream(prompt, stop, **kwargs):
    416     if not isinstance(stream_resp, str):
    417         if stream_resp.get("thinking"):

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/langchain_ollama/llms.py:359, in OllamaLLM._create_generate_stream(self, prompt, stop, **kwargs)
    352 def _create_generate_stream(
    353     self,
    354     prompt: str,
    355     stop: list[str] | None = None,
    356     **kwargs: Any,
    357 ) -> Iterator[Mapping[str, Any] | str]:
    358     if self._client:
--> 359         yield from self._client.generate(
    360             **self._generate_params(prompt, stop=stop, **kwargs)
    361         )

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/ollama/_client.py:174, in Client._request.<locals>.inner()
    173 def inner():
--> 174   with self._client.stream(*args, **kwargs) as r:
    175     try:
    176       r.raise_for_status()

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/contextlib.py:137, in _GeneratorContextManager.__enter__(self)
    135 del self.args, self.kwds, self.func
    136 try:
--> 137     return next(self.gen)
    138 except StopIteration:
    139     raise RuntimeError("generator didn't yield") from None

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_client.py:868, in Client.stream(self, method, url, content, data, files, json, params, headers, cookies, auth, follow_redirects, timeout, extensions)
    845 """
    846 Alternative to `httpx.request()` that streams the response body
    847 instead of loading it into memory at once.
   (...)    853 [0]: /quickstart#streaming-responses
    854 """
    855 request = self.build_request(
    856     method=method,
    857     url=url,
   (...)    866     extensions=extensions,
    867 )
--> 868 response = self.send(
    869     request=request,
    870     auth=auth,
    871     follow_redirects=follow_redirects,
    872     stream=True,
    873 )
    874 try:
    875     yield response

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_client.py:914, in Client.send(self, request, stream, auth, follow_redirects)
    910 self._set_timeout(request)
    912 auth = self._build_request_auth(request, auth)
--> 914 response = self._send_handling_auth(
    915     request,
    916     auth=auth,
    917     follow_redirects=follow_redirects,
    918     history=[],
    919 )
    920 try:
    921     if not stream:

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_client.py:942, in Client._send_handling_auth(self, request, auth, follow_redirects, history)
    939 request = next(auth_flow)
    941 while True:
--> 942     response = self._send_handling_redirects(
    943         request,
    944         follow_redirects=follow_redirects,
    945         history=history,
    946     )
    947     try:
    948         try:

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_client.py:979, in Client._send_handling_redirects(self, request, follow_redirects, history)
    976 for hook in self._event_hooks["request"]:
    977     hook(request)
--> 979 response = self._send_single_request(request)
    980 try:
    981     for hook in self._event_hooks["response"]:

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_client.py:1014, in Client._send_single_request(self, request)
   1009     raise RuntimeError(
   1010         "Attempted to send an async request with a sync Client instance."
   1011     )
   1013 with request_context(request=request):
-> 1014     response = transport.handle_request(request)
   1016 assert isinstance(response.stream, SyncByteStream)
   1018 response.request = request

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_transports/default.py:249, in HTTPTransport.handle_request(self, request)
    235 import httpcore
    237 req = httpcore.Request(
    238     method=request.method,
    239     url=httpcore.URL(
   (...)    247     extensions=request.extensions,
    248 )
--> 249 with map_httpcore_exceptions():
    250     resp = self._pool.handle_request(req)
    252 assert isinstance(resp.stream, typing.Iterable)

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    156     value = typ()
    157 try:
--> 158     self.gen.throw(value)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.
    163     return exc is not value

File /opt/homebrew/Caskroom/miniconda/base/envs/codesolid/lib/python3.12/site-packages/httpx/_transports/default.py:118, in map_httpcore_exceptions()
    115     raise
    117 message = str(exc)
--> 118 raise mapped_exc(message) from exc

ConnectError: [Errno 61] Connection refused