Skip to content

Commit e686a70

Browse files
authored
ollama: thinking, tool streaming, docs, tests (#31772)
* New `reasoning` (bool) param to support toggling [Ollama thinking](https://ollama.com/blog/thinking) (#31573, #31700). If `reasoning=True`, Ollama's `thinking` content will be placed in the model responses' `additional_kwargs.reasoning_content`. * Supported by: * ChatOllama (class level, invocation level TODO) * OllamaLLM (TODO) * Added tests to ensure streaming tool calls is successful (#29129) * Refactored tests that relied on `extract_reasoning()` * Myriad docs additions and consistency/typo fixes * Improved type safety in some spots Closes #29129 Addresses #31573 and #31700 Supersedes #31701
1 parent 0eb10f3 commit e686a70

File tree

14 files changed

+628
-211
lines changed

14 files changed

+628
-211
lines changed

docs/docs/how_to/callbacks_custom_events.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"\n",
3939
"\n",
4040
":::caution COMPATIBILITY\n",
41-
"LangChain cannot automatically propagate configuration, including callbacks necessary for astream_events(), to child runnables if you are running async code in python<=3.10. This is a common reason why you may fail to see events being emitted from custom runnables or tools.\n",
41+
"LangChain cannot automatically propagate configuration, including callbacks necessary for astream_events(), to child runnables if you are running async code in `python<=3.10`. This is a common reason why you may fail to see events being emitted from custom runnables or tools.\n",
4242
"\n",
43-
"If you are running python&lt;=3.10, you will need to manually propagate the `RunnableConfig` object to the child runnable in async environments. For an example of how to manually propagate the config, see the implementation of the `bar` RunnableLambda below.\n",
43+
"If you are running `python<=3.10`, you will need to manually propagate the `RunnableConfig` object to the child runnable in async environments. For an example of how to manually propagate the config, see the implementation of the `bar` RunnableLambda below.\n",
4444
"\n",
45-
"If you are running python>=3.11, the `RunnableConfig` will automatically propagate to child runnables in async environment. However, it is still a good idea to propagate the `RunnableConfig` manually if your code may run in other Python versions.\n",
45+
"If you are running `python>=3.11`, the `RunnableConfig` will automatically propagate to child runnables in async environment. However, it is still a good idea to propagate the `RunnableConfig` manually if your code may run in other Python versions.\n",
4646
":::"
4747
]
4848
},

docs/docs/how_to/tool_stream_events.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
"\n",
1717
":::\n",
1818
"\n",
19-
"If you have [tools](/docs/concepts/tools/) that call [chat models](/docs/concepts/chat_models/), [retrievers](/docs/concepts/retrievers/), or other [runnables](/docs/concepts/runnables/), you may want to access internal events from those runnables or configure them with additional properties. This guide shows you how to manually pass parameters properly so that you can do this using the `astream_events()` method.\n",
19+
"If you have [tools](/docs/concepts/tools/) that call [chat models](/docs/concepts/chat_models/), [retrievers](/docs/concepts/retrievers/), or other [runnables](/docs/concepts/runnables/), you may want to access [internal events](https://python.langchain.com/docs/how_to/streaming/#event-reference) from those runnables or configure them with additional properties. This guide shows you how to manually pass parameters properly so that you can do this using the `astream_events()` method.\n",
2020
"\n",
2121
":::caution Compatibility\n",
2222
"\n",
23-
"LangChain cannot automatically propagate configuration, including callbacks necessary for `astream_events()`, to child runnables if you are running `async` code in `python&lt;=3.10`. This is a common reason why you may fail to see events being emitted from custom runnables or tools.\n",
23+
"LangChain cannot automatically propagate configuration, including callbacks necessary for `astream_events()`, to child runnables if you are running `async` code in `python<=3.10`. This is a common reason why you may fail to see events being emitted from custom runnables or tools.\n",
2424
"\n",
25-
"If you are running python&lt;=3.10, you will need to manually propagate the `RunnableConfig` object to the child runnable in async environments. For an example of how to manually propagate the config, see the implementation of the `bar` RunnableLambda below.\n",
25+
"If you are running `python<=3.10`, you will need to manually propagate the `RunnableConfig` object to the child runnable in async environments. For an example of how to manually propagate the config, see the implementation of the `bar` RunnableLambda below.\n",
2626
"\n",
27-
"If you are running python>=3.11, the `RunnableConfig` will automatically propagate to child runnables in async environment. However, it is still a good idea to propagate the `RunnableConfig` manually if your code may run in older Python versions.\n",
27+
"If you are running `python>=3.11`, the `RunnableConfig` will automatically propagate to child runnables in async environment. However, it is still a good idea to propagate the `RunnableConfig` manually if your code may run in older Python versions.\n",
2828
"\n",
2929
"This guide also requires `langchain-core>=0.2.16`.\n",
3030
":::\n",

docs/docs/how_to/tool_streaming.ipynb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@
224224
"source": [
225225
"print(type(gathered.tool_calls[0][\"args\"]))"
226226
]
227+
},
228+
{
229+
"cell_type": "markdown",
230+
"metadata": {},
231+
"source": [
232+
"Note the key difference: accumulating `tool_call_chunks` captures the raw tool arguments as an unparsed string as they are streamed. In contrast, **accumulating** `tool_calls` demonstrates partial parsing by progressively converting the streamed argument string into a valid, usable dictionary at each step of the process."
233+
]
227234
}
228235
],
229236
"metadata": {

libs/core/langchain_core/language_models/llms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,9 +1414,10 @@ def save(self, file_path: Union[Path, str]) -> None:
14141414
ValueError: If the file path is not a string or Path object.
14151415
14161416
Example:
1417-
.. code-block:: python
14181417
1419-
llm.save(file_path="path/llm.yaml")
1418+
.. code-block:: python
1419+
1420+
llm.save(file_path="path/llm.yaml")
14201421
"""
14211422
# Convert file to Path object.
14221423
save_path = Path(file_path)

libs/core/langchain_core/runnables/base.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,30 +1160,29 @@ async def astream_events(
11601160
11611161
A StreamEvent is a dictionary with the following schema:
11621162
1163-
- ``event``: **str** - Event names are of the
1164-
format: on_[runnable_type]_(start|stream|end).
1163+
- ``event``: **str** - Event names are of the format:
1164+
on_[runnable_type]_(start|stream|end).
11651165
- ``name``: **str** - The name of the Runnable that generated the event.
1166-
- ``run_id``: **str** - randomly generated ID associated with the given execution of
1167-
the Runnable that emitted the event.
1168-
A child Runnable that gets invoked as part of the execution of a
1169-
parent Runnable is assigned its own unique ID.
1170-
- ``parent_ids``: **list[str]** - The IDs of the parent runnables that
1171-
generated the event. The root Runnable will have an empty list.
1172-
The order of the parent IDs is from the root to the immediate parent.
1173-
Only available for v2 version of the API. The v1 version of the API
1174-
will return an empty list.
1166+
- ``run_id``: **str** - randomly generated ID associated with the given
1167+
execution of the Runnable that emitted the event. A child Runnable that gets
1168+
invoked as part of the execution of a parent Runnable is assigned its own
1169+
unique ID.
1170+
- ``parent_ids``: **list[str]** - The IDs of the parent runnables that generated
1171+
the event. The root Runnable will have an empty list. The order of the parent
1172+
IDs is from the root to the immediate parent. Only available for v2 version of
1173+
the API. The v1 version of the API will return an empty list.
11751174
- ``tags``: **Optional[list[str]]** - The tags of the Runnable that generated
1176-
the event.
1177-
- ``metadata``: **Optional[dict[str, Any]]** - The metadata of the Runnable
1178-
that generated the event.
1175+
the event.
1176+
- ``metadata``: **Optional[dict[str, Any]]** - The metadata of the Runnable that
1177+
generated the event.
11791178
- ``data``: **dict[str, Any]**
11801179
11811180
11821181
Below is a table that illustrates some events that might be emitted by various
11831182
chains. Metadata fields have been omitted from the table for brevity.
11841183
Chain definitions have been included after the table.
11851184
1186-
**ATTENTION** This reference table is for the V2 version of the schema.
1185+
.. NOTE:: This reference table is for the V2 version of the schema.
11871186
11881187
+----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
11891188
| event | name | chunk | input | output |

0 commit comments

Comments
 (0)