Compare commits

..

5 Commits

Author SHA1 Message Date
Mason Daugherty
1dcebc7101 Merge branch 'master' into eugene/update_tools_schema 2025-08-11 18:23:47 -04:00
Eugene Yurtsev
fd532c692d x 2025-06-20 13:17:12 -04:00
Eugene Yurtsev
97db95b426 x 2025-06-20 13:16:40 -04:00
Eugene Yurtsev
b9ebae37a4 x 2025-06-20 13:14:10 -04:00
Eugene Yurtsev
d3930f9906 x 2025-06-20 12:41:21 -04:00
204 changed files with 3046 additions and 4930 deletions

View File

@@ -7,4 +7,4 @@ To learn how to contribute to LangChain, please follow the [contribution guide h
## New features
For new features, please start a new [discussion on our forum](https://forum.langchain.com/), where the maintainers will help with scoping out the necessary changes.
For new features, please start a new [discussion](https://forum.langchain.com/), where the maintainers will help with scoping out the necessary changes.

View File

@@ -9,13 +9,15 @@
</div>
[![Release Notes](https://img.shields.io/github/release/langchain-ai/langchain?style=flat-square)](https://github.com/langchain-ai/langchain/releases)
[![CI](https://github.com/langchain-ai/langchain/actions/workflows/check_diffs.yml/badge.svg)](https://github.com/langchain-ai/langchain/actions/workflows/check_diffs.yml)
[![PyPI - License](https://img.shields.io/pypi/l/langchain-core?style=flat-square)](https://opensource.org/licenses/MIT)
[![PyPI - Downloads](https://img.shields.io/pepy/dt/langchain)](https://pypistats.org/packages/langchain-core)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-core?style=flat-square)](https://pypistats.org/packages/langchain-core)
[![GitHub star chart](https://img.shields.io/github/stars/langchain-ai/langchain?style=flat-square)](https://star-history.com/#langchain-ai/langchain)
[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langchain?style=flat-square)](https://github.com/langchain-ai/langchain/issues)
[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode&style=flat-square)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/langchain-ai/langchain)
[<img src="https://github.com/codespaces/badge.svg" alt="Open in Github Codespace" title="Open in Github Codespace" width="150" height="20">](https://codespaces.new/langchain-ai/langchain)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/langchain-ai/langchain)
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/langchainai.svg?style=social&label=Follow%20%40LangChainAI)](https://twitter.com/langchainai)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/langchain-ai/langchain)
> [!NOTE]
> Looking for the JS/TS library? Check out [LangChain.js](https://github.com/langchain-ai/langchainjs).
@@ -66,7 +68,7 @@ reliably handle complex tasks with LangGraph, our low-level agent orchestration
framework. LangGraph offers customizable architecture, long-term memory, and
human-in-the-loop workflows — and is trusted in production by companies like LinkedIn,
Uber, Klarna, and GitLab.
- [LangGraph Platform](https://docs.langchain.com/langgraph-platform) - Deploy
- [LangGraph Platform](https://langchain-ai.github.io/langgraph/concepts/langgraph_platform/) - Deploy
and scale agents effortlessly with a purpose-built deployment platform for long
running, stateful workflows. Discover, reuse, configure, and share agents across
teams — and iterate quickly with visual prototyping in
@@ -83,4 +85,3 @@ concepts behind the LangChain framework.
- [LangChain Forum](https://forum.langchain.com/): Connect with the community and share all of your technical questions, ideas, and feedback.
- [API Reference](https://python.langchain.com/api_reference/): Detailed reference on
navigating base packages and integrations for LangChain.
- [Chat LangChain](https://chat.langchain.com/): Ask questions & chat with our documentation

View File

@@ -147,7 +147,7 @@ An `AIMessage` has the following attributes. The attributes which are **standard
| `tool_calls` | Standardized | Tool calls associated with the message. See [tool calling](/docs/concepts/tool_calling) for details. |
| `invalid_tool_calls` | Standardized | Tool calls with parsing errors associated with the message. See [tool calling](/docs/concepts/tool_calling) for details. |
| `usage_metadata` | Standardized | Usage metadata for a message, such as [token counts](/docs/concepts/tokens). See [Usage Metadata API Reference](https://python.langchain.com/api_reference/core/messages/langchain_core.messages.ai.UsageMetadata.html). |
| `id` | Standardized | An optional unique identifier for the message, ideally provided by the provider/model that created the message. See [Message IDs](#message-ids) for details. |
| `id` | Standardized | An optional unique identifier for the message, ideally provided by the provider/model that created the message. |
| `response_metadata` | Raw | Response metadata, e.g., response headers, logprobs, token counts. |
#### content
@@ -243,37 +243,3 @@ At the moment, the output of the model will be in terms of LangChain messages, s
need OpenAI format for the output as well.
The [convert_to_openai_messages](https://python.langchain.com/api_reference/core/messages/langchain_core.messages.utils.convert_to_openai_messages.html) utility function can be used to convert from LangChain messages to OpenAI format.
## Message IDs
LangChain messages include an optional `id` field that serves as a unique identifier. Understanding when and how these IDs are assigned can be helpful for debugging, tracing, and working with message history.
### When Messages Get IDs
Messages receive IDs in the following scenarios:
**Automatically assigned by LangChain:**
- When generated through chat model invocation (`.invoke()`, `.stream()`, `.astream()`) with an active run manager/tracing context
- IDs follow the format:
- `run-$RUN_ID` (e.g., `run-ba48f958-6402-41a5-b461-5e250a4ebd36-0`)
- `run-$RUN_ID-$IDX` (e.g., `run-ba48f958-6402-41a5-b461-5e250a4ebd36-1`) when there are multiple generations from a single chat model invocation.
**Provider-assigned IDs (highest priority):**
- When the model provider assigns its own ID to the message
- These take precedence over LangChain-generated run IDs
- Format varies by provider
### When Messages Don't Get IDs
Messages will **not** receive IDs in these situations:
- **Manual message creation**: Messages created directly (e.g., `AIMessage(content="hello")`) without going through chat models
- **No run manager context**: When there's no active callback/tracing infrastructure
### ID Priority System
LangChain follows a clear precedence system for message IDs:
1. **Provider-assigned IDs** (highest priority): IDs from the model provider
2. **LangChain run IDs** (medium priority): IDs starting with `run-`
3. **Manual IDs** (lowest priority): IDs explicitly set by users

View File

@@ -29,22 +29,6 @@ model_with_structure = model.with_structured_output(schema)
structured_output = model_with_structure.invoke(user_input)
```
:::warning[Tool Order Matters]
When combining structured output with additional tools, bind tools **first**, then apply structured output:
```python
# Correct
model_with_tools = model.bind_tools([tool1, tool2])
structured_model = model_with_tools.with_structured_output(schema)
# Incorrect - will cause tool resolution errors
structured_model = model.with_structured_output(schema)
broken_model = structured_model.bind_tools([tool1, tool2])
```
:::
## Schema definition
The central concept is that the output structure of model responses needs to be represented in some way.

View File

@@ -223,49 +223,6 @@ If codespell is incorrectly flagging a word, you can skip spellcheck for that wo
ignore-words-list = 'momento,collison,ned,foor,reworkd,parth,whats,aapply,mysogyny,unsecure'
```
### Pre-commit
We use [pre-commit](https://pre-commit.com/) to ensure commits are formatted/linted.
#### Installing Pre-commit
First, install pre-commit:
```bash
# Option 1: Using uv (recommended)
uv tool install pre-commit
# Option 2: Using Homebrew (globally for macOS/Linux)
brew install pre-commit
# Option 3: Using pip
pip install pre-commit
```
Then install the git hook scripts:
```bash
pre-commit install
```
#### How Pre-commit Works
Once installed, pre-commit will automatically run on every `git commit`. Hooks are specified in `.pre-commit-config.yaml` and will:
- Format code using `ruff` for the specific library/package you're modifying
- Only run on files that have changed
- Prevent commits if formatting fails
#### Skipping Pre-commit
In exceptional cases, you can skip pre-commit hooks with:
```bash
git commit --no-verify
```
However, this is discouraged as the CI system will still enforce the same formatting rules.
## Working with optional dependencies
`langchain`, `langchain-community`, and `langchain-experimental` rely on optional dependencies to keep these packages lightweight.

View File

@@ -159,7 +159,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "321e3036-abd2-4e1f-bcc6-606efd036954",
"metadata": {
"execution": {
@@ -183,7 +183,7 @@
],
"source": [
"configurable_model.invoke(\n",
" \"what's your name\", config={\"configurable\": {\"model\": \"claude-3-5-sonnet-latest\"}}\n",
" \"what's your name\", config={\"configurable\": {\"model\": \"claude-3-5-sonnet-20240620\"}}\n",
")"
]
},
@@ -234,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "6c8755ba-c001-4f5a-a497-be3f1db83244",
"metadata": {
"execution": {
@@ -261,7 +261,7 @@
" \"what's your name\",\n",
" config={\n",
" \"configurable\": {\n",
" \"first_model\": \"claude-3-5-sonnet-latest\",\n",
" \"first_model\": \"claude-3-5-sonnet-20240620\",\n",
" \"first_temperature\": 0.5,\n",
" \"first_max_tokens\": 100,\n",
" }\n",
@@ -336,7 +336,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "e57dfe9f-cd24-4e37-9ce9-ccf8daf78f89",
"metadata": {
"execution": {
@@ -368,14 +368,14 @@
"source": [
"llm_with_tools.invoke(\n",
" \"what's bigger in 2024 LA or NYC\",\n",
" config={\"configurable\": {\"model\": \"claude-3-5-sonnet-latest\"}},\n",
" config={\"configurable\": {\"model\": \"claude-3-5-sonnet-20240620\"}},\n",
").tool_calls"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "langchain-monorepo",
"display_name": "langchain",
"language": "python",
"name": "python3"
},
@@ -389,7 +389,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.11"
"version": "3.10.16"
}
},
"nbformat": 4,

View File

@@ -741,13 +741,13 @@
"\n",
"If you're using tools with agents, you will likely need an error handling strategy, so the agent can recover from the error and continue execution.\n",
"\n",
"A simple strategy is to throw a `ToolException` from inside the tool and specify an error handler using `handle_tool_errors`. \n",
"A simple strategy is to throw a `ToolException` from inside the tool and specify an error handler using `handle_tool_error`. \n",
"\n",
"When the error handler is specified, the exception will be caught and the error handler will decide which output to return from the tool.\n",
"\n",
"You can set `handle_tool_errors` to `True`, a string value, or a function. If it's a function, the function should take a `ToolException` as a parameter and return a value.\n",
"You can set `handle_tool_error` to `True`, a string value, or a function. If it's a function, the function should take a `ToolException` as a parameter and return a value.\n",
"\n",
"Please note that only raising a `ToolException` won't be effective. You need to first set the `handle_tool_errors` of the tool because its default value is `False`."
"Please note that only raising a `ToolException` won't be effective. You need to first set the `handle_tool_error` of the tool because its default value is `False`."
]
},
{
@@ -777,7 +777,7 @@
"id": "9d93b217-1d44-4d31-8956-db9ea680ff4f",
"metadata": {},
"source": [
"Here's an example with the default `handle_tool_errors=True` behavior."
"Here's an example with the default `handle_tool_error=True` behavior."
]
},
{
@@ -807,7 +807,7 @@
"source": [
"get_weather_tool = StructuredTool.from_function(\n",
" func=get_weather,\n",
" handle_tool_errors=True,\n",
" handle_tool_error=True,\n",
")\n",
"\n",
"get_weather_tool.invoke({\"city\": \"foobar\"})"
@@ -818,7 +818,7 @@
"id": "f91d6dc0-3271-4adc-a155-21f2e62ffa56",
"metadata": {},
"source": [
"We can set `handle_tool_errors` to a string that will always be returned."
"We can set `handle_tool_error` to a string that will always be returned."
]
},
{
@@ -848,7 +848,7 @@
"source": [
"get_weather_tool = StructuredTool.from_function(\n",
" func=get_weather,\n",
" handle_tool_errors=\"There is no such city, but it's probably above 0K there!\",\n",
" handle_tool_error=\"There is no such city, but it's probably above 0K there!\",\n",
")\n",
"\n",
"get_weather_tool.invoke({\"city\": \"foobar\"})"
@@ -893,7 +893,7 @@
"\n",
"get_weather_tool = StructuredTool.from_function(\n",
" func=get_weather,\n",
" handle_tool_errors=_handle_error,\n",
" handle_tool_error=_handle_error,\n",
")\n",
"\n",
"get_weather_tool.invoke({\"city\": \"foobar\"})"

View File

@@ -565,7 +565,7 @@
"id": "3ac2c37a-06a1-40d3-a192-9078eb83994b",
"metadata": {},
"source": [
"<table><thead><tr><th colspan=\"3\">Table 1: Current layout detection models in the LayoutParser model zoo</th></tr><tr><th>Dataset</th><th>Base Model1</th><th>Large Model Notes</th></tr></thead><tbody><tr><td>PubLayNet [38]</td><td>F/M</td><td>Layouts of modern scientific documents</td></tr><tr><td>PRImA</td><td>M</td><td>Layouts of scanned modern magazines and scientific reports</td></tr><tr><td>Newspaper</td><td>F</td><td>Layouts of scanned US newspapers from the 20th century</td></tr><tr><td>TableBank [18]</td><td>F</td><td>Table region on modern scientific and business document</td></tr><tr><td>HJDataset</td><td>F/M</td><td>Layouts of history Japanese documents</td></tr></tbody></table>"
"<table><thead><tr><th colspan=\"3\">able 1. LUllclll 1ayoul actCCLloll 1110AdCs 111 L1C LayoOulralsel 1110U4cl 200</th></tr><tr><th>Dataset</th><th>| Base Model\\'|</th><th>Notes</th></tr></thead><tbody><tr><td>PubLayNet [38]</td><td>F/M</td><td>Layouts of modern scientific documents</td></tr><tr><td>PRImA</td><td>M</td><td>Layouts of scanned modern magazines and scientific reports</td></tr><tr><td>Newspaper</td><td>F</td><td>Layouts of scanned US newspapers from the 20th century</td></tr><tr><td>TableBank [18]</td><td>F</td><td>Table region on modern scientific and business document</td></tr><tr><td>HJDataset</td><td>F/M</td><td>Layouts of history Japanese documents</td></tr></tbody></table>"
]
},
{

View File

@@ -47,7 +47,7 @@ See [supported integrations](/docs/integrations/chat/) for details on getting st
- [How to: use chat model to call tools](/docs/how_to/tool_calling)
- [How to: stream tool calls](/docs/how_to/tool_streaming)
- [How to: handle rate limits](/docs/how_to/chat_model_rate_limiting)
- [How to: few-shot prompt tool behavior](/docs/how_to/tools_few_shot)
- [How to: few shot prompt tool behavior](/docs/how_to/tools_few_shot)
- [How to: bind model-specific formatted tools](/docs/how_to/tools_model_specific)
- [How to: force a specific tool call](/docs/how_to/tool_choice)
- [How to: pass multimodal data directly to models](/docs/how_to/multimodal_inputs/)
@@ -64,8 +64,8 @@ See [supported integrations](/docs/integrations/chat/) for details on getting st
[Prompt Templates](/docs/concepts/prompt_templates) are responsible for formatting user input into a format that can be passed to a language model.
- [How to: use few-shot examples](/docs/how_to/few_shot_examples)
- [How to: use few-shot examples in chat models](/docs/how_to/few_shot_examples_chat/)
- [How to: use few shot examples](/docs/how_to/few_shot_examples)
- [How to: use few shot examples in chat models](/docs/how_to/few_shot_examples_chat/)
- [How to: partially format prompt templates](/docs/how_to/prompts_partial)
- [How to: compose prompts together](/docs/how_to/prompts_composition)
- [How to: use multimodal prompts](/docs/how_to/multimodal_prompts/)
@@ -168,7 +168,7 @@ See [supported integrations](/docs/integrations/vectorstores/) for details on ge
Indexing is the process of keeping your vectorstore in-sync with the underlying data source.
- [How to: reindex data to keep your vectorstore in sync with the underlying data source](/docs/how_to/indexing)
- [How to: reindex data to keep your vectorstore in-sync with the underlying data source](/docs/how_to/indexing)
### Tools
@@ -178,7 +178,7 @@ LangChain [Tools](/docs/concepts/tools) contain a description of the tool (to pa
- [How to: use built-in tools and toolkits](/docs/how_to/tools_builtin)
- [How to: use chat models to call tools](/docs/how_to/tool_calling)
- [How to: pass tool outputs to chat models](/docs/how_to/tool_results_pass_to_model)
- [How to: pass runtime values to tools](/docs/how_to/tool_runtime)
- [How to: pass run time values to tools](/docs/how_to/tool_runtime)
- [How to: add a human-in-the-loop for tools](/docs/how_to/tools_human)
- [How to: handle tool errors](/docs/how_to/tools_error)
- [How to: force models to call a tool](/docs/how_to/tool_choice)
@@ -297,7 +297,7 @@ For a high-level tutorial, check out [this guide](/docs/tutorials/sql_qa/).
You can use an LLM to do question answering over graph databases.
For a high-level tutorial, check out [this guide](/docs/tutorials/graph/).
- [How to: add a semantic layer over a database](/docs/how_to/graph_semantic)
- [How to: add a semantic layer over the database](/docs/how_to/graph_semantic)
- [How to: construct knowledge graphs](/docs/how_to/graph_constructing)
### Summarization
@@ -345,7 +345,7 @@ LangGraph is an extension of LangChain aimed at
building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph.
LangGraph documentation is currently hosted on a separate site.
You can find the [LangGraph guides here](https://langchain-ai.github.io/langgraph/guides/).
You can peruse [LangGraph how-to guides here](https://langchain-ai.github.io/langgraph/how-tos/).
## [LangSmith](https://docs.smith.langchain.com/)

View File

@@ -998,91 +998,6 @@
"\n",
"chain.invoke({\"query\": query})"
]
},
{
"cell_type": "markdown",
"id": "xfejabhtn2",
"metadata": {},
"source": [
"## Combining with Additional Tools\n",
"\n",
"When you need to use both structured output and additional tools (like web search), note the order of operations:\n",
"\n",
"**Correct Order**:\n",
"```python\n",
"# 1. Bind tools first\n",
"llm_with_tools = llm.bind_tools([web_search_tool, calculator_tool])\n",
"\n",
"# 2. Apply structured output\n",
"structured_llm = llm_with_tools.with_structured_output(MySchema)\n",
"```\n",
"\n",
"**Incorrect Order**:\n",
"\n",
"```python\n",
"# This will fail with \"Tool 'MySchema' not found\" error\n",
"structured_llm = llm.with_structured_output(MySchema)\n",
"broken_llm = structured_llm.bind_tools([web_search_tool])\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "653798ca",
"metadata": {},
"source": [
"**Why Order Matters:**\n",
"`with_structured_output()` internally uses tool calling to enforce the schema. When you bind additional tools afterward, it creates a conflict in the tool resolution system."
]
},
{
"cell_type": "markdown",
"id": "1345f4a4",
"metadata": {},
"source": [
"**Complete Example:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0835637b",
"metadata": {},
"outputs": [],
"source": [
"from pydantic import BaseModel, Field\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"\n",
"class SearchResult(BaseModel):\n",
" \"\"\"Structured search result.\"\"\"\n",
"\n",
" query: str = Field(description=\"The search query\")\n",
" findings: str = Field(description=\"Summary of findings\")\n",
"\n",
"\n",
"# Define tools\n",
"search_tool = {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"web_search\",\n",
" \"description\": \"Search the web for information\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\"query\": {\"type\": \"string\", \"description\": \"Search query\"}},\n",
" \"required\": [\"query\"],\n",
" },\n",
" },\n",
"}\n",
"\n",
"# Correct approach\n",
"llm = ChatOpenAI()\n",
"llm_with_search = llm.bind_tools([search_tool])\n",
"structured_search_llm = llm_with_search.with_structured_output(SearchResult)\n",
"\n",
"# Now you can use both search and get structured output\n",
"result = structured_search_llm.invoke(\"Search for latest AI research and summarize\")"
]
}
],
"metadata": {

View File

@@ -147,7 +147,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "74de0286-b003-4b48-9cdd-ecab435515ca",
"metadata": {},
"outputs": [],
@@ -157,7 +157,7 @@
"\n",
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"llm = ChatAnthropic(model=\"claude-3-5-sonnet-latest\", temperature=0)"
"llm = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\", temperature=0)"
]
},
{

View File

@@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -53,7 +53,7 @@
"if \"ANTHROPIC_API_KEY\" not in os.environ:\n",
" os.environ[\"ANTHROPIC_API_KEY\"] = getpass()\n",
"\n",
"model = ChatAnthropic(model=\"claude-3-5-sonnet-latest\", temperature=0)"
"model = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\", temperature=0)"
]
},
{

View File

@@ -53,7 +53,7 @@
"\n",
"To keep the most recent messages, we set `strategy=\"last\"`. We'll also set `include_system=True` to include the `SystemMessage`, and `start_on=\"human\"` to make sure the resulting chat history is valid. \n",
"\n",
"This is a good default configuration when using `trim_messages` based on token count. Remember to adjust `token_counter` and `max_tokens` for your use case. Keep in mind that new queries added to the chat history will be included in the token count unless you trim prior to adding the new query.\n",
"This is a good default configuration when using `trim_messages` based on token count. Remember to adjust `token_counter` and `max_tokens` for your use case.\n",
"\n",
"Notice that for our `token_counter` we can pass in a function (more on that below) or a language model (since language models have a message token counting method). It makes sense to pass in a model when you're trimming your messages to fit into the context window of that specific model:"
]
@@ -525,7 +525,7 @@
"id": "4d91d390-e7f7-467b-ad87-d100411d7a21",
"metadata": {},
"source": [
"Looking at [the LangSmith trace](https://smith.langchain.com/public/65af12c4-c24d-4824-90f0-6547566e59bb/r) we can see that before the messages are passed to the model they are first trimmed.\n",
"Looking at the LangSmith trace we can see that before the messages are passed to the model they are first trimmed: https://smith.langchain.com/public/65af12c4-c24d-4824-90f0-6547566e59bb/r\n",
"\n",
"Looking at just the trimmer, we can see that it's a Runnable object that can be invoked like all Runnables:"
]
@@ -620,7 +620,7 @@
"id": "556b7b4c-43cb-41de-94fc-1a41f4ec4d2e",
"metadata": {},
"source": [
"Looking at [the LangSmith trace](https://smith.langchain.com/public/17dd700b-9994-44ca-930c-116e00997315/r) we can see that we retrieve all of our messages but before the messages are passed to the model they are trimmed to be just the system message and last human message."
"Looking at the LangSmith trace we can see that we retrieve all of our messages but before the messages are passed to the model they are trimmed to be just the system message and last human message: https://smith.langchain.com/public/17dd700b-9994-44ca-930c-116e00997315/r"
]
},
{
@@ -630,7 +630,7 @@
"source": [
"## API reference\n",
"\n",
"For a complete description of all arguments head to the [API reference](https://python.langchain.com/api_reference/core/messages/langchain_core.messages.utils.trim_messages.html)."
"For a complete description of all arguments head to the API reference: https://python.langchain.com/api_reference/core/messages/langchain_core.messages.utils.trim_messages.html"
]
}
],

View File

@@ -124,7 +124,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
@@ -132,7 +132,7 @@
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"llm = ChatAnthropic(\n",
" model=\"claude-3-5-sonnet-latest\",\n",
" model=\"claude-3-5-sonnet-20240620\",\n",
" temperature=0,\n",
" max_tokens=1024,\n",
" timeout=None,\n",
@@ -1240,58 +1240,6 @@
"response = llm_with_tools.invoke(\"How do I update a web app to TypeScript 5.5?\")"
]
},
{
"cell_type": "markdown",
"id": "kloc4rvd1w",
"metadata": {},
"source": [
"#### Web search + structured output\n",
"\n",
"When combining web search tools with structured output, it's important to **bind the tools first and then apply structured output**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "rjjergy6ef",
"metadata": {},
"outputs": [],
"source": [
"from pydantic import BaseModel, Field\n",
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"\n",
"# Define structured output schema\n",
"class ResearchResult(BaseModel):\n",
" \"\"\"Structured research result from web search.\"\"\"\n",
"\n",
" topic: str = Field(description=\"The research topic\")\n",
" summary: str = Field(description=\"Summary of key findings\")\n",
" key_points: list[str] = Field(description=\"List of important points discovered\")\n",
"\n",
"\n",
"# Configure web search tool\n",
"websearch_tools = [\n",
" {\n",
" \"type\": \"web_search_20250305\",\n",
" \"name\": \"web_search\",\n",
" \"max_uses\": 10,\n",
" }\n",
"]\n",
"\n",
"llm = ChatAnthropic(model=\"claude-3-5-sonnet-20241022\")\n",
"\n",
"# Correct order: bind tools first, then structured output\n",
"llm_with_search = llm.bind_tools(websearch_tools)\n",
"research_llm = llm_with_search.with_structured_output(ResearchResult)\n",
"\n",
"# Now you can use both web search and get structured output\n",
"result = research_llm.invoke(\"Research the latest developments in quantum computing\")\n",
"print(f\"Topic: {result.topic}\")\n",
"print(f\"Summary: {result.summary}\")\n",
"print(f\"Key Points: {result.key_points}\")"
]
},
{
"cell_type": "markdown",
"id": "1478cdc6-2e52-4870-80f9-b4ddf88f2db2",

View File

@@ -129,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
@@ -137,7 +137,7 @@
"from langchain_aws import ChatBedrockConverse\n",
"\n",
"llm = ChatBedrockConverse(\n",
" model_id=\"anthropic.claude-3-5-sonnet-latest-v1:0\",\n",
" model_id=\"anthropic.claude-3-5-sonnet-20240620-v1:0\",\n",
" # region_name=...,\n",
" # aws_access_key_id=...,\n",
" # aws_secret_access_key=...,\n",

View File

@@ -53,7 +53,7 @@
"source": [
"### Installation\n",
"\n",
"The LangChain OCIGenAI integration lives in the `langchain-oci` package and you will also need to install the `oci` package:"
"The LangChain OCIGenAI integration lives in the `langchain-community` package and you will also need to install the `oci` package:"
]
},
{
@@ -63,7 +63,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-oci"
"%pip install -qU langchain-community oci"
]
},
{
@@ -83,7 +83,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_oci.chat_models import ChatOCIGenAI\n",
"from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI\n",
"from langchain_core.messages import AIMessage, HumanMessage, SystemMessage\n",
"\n",
"chat = ChatOCIGenAI(\n",

View File

@@ -17,7 +17,7 @@
"source": [
"# ChatOllama\n",
"\n",
"[Ollama](https://ollama.com/) allows you to run open-source large language models, such as `gpt-oss`, locally.\n",
"[Ollama](https://ollama.com/) allows you to run open-source large language models, such as `got-oss`, locally.\n",
"\n",
"`ollama` bundles model weights, configuration, and data into a single package, defined by a Modelfile.\n",
"\n",

View File

@@ -1,334 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Oxylabs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Oxylabs](https://oxylabs.io/) is a web intelligence collection platform that enables companies worldwide to unlock data-driven insights.\n",
"\n",
"## Overview\n",
"\n",
"Oxylabs document loader allows to load data from search engines, e-commerce sites, travel platforms, and any other website. It supports geolocation, browser rendering, data parsing, multiple user agents and many more parameters. Check out [Oxylabs documentation](https://developers.oxylabs.io/scraping-solutions/web-scraper-api) for more information.\n",
"\n",
"\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | Pricing |\n",
"|:--------------|:------------------------------------------------------------------|:-----:|:------------:|:-----------------------------:|\n",
"| OxylabsLoader | [langchain-oxylabs](https://github.com/oxylabs/langchain-oxylabs) | ✅ | ❌ | Free 5,000 results for 1 week |\n",
"\n",
"### Loader features\n",
"| Document Lazy Loading |\n",
"|:---------------------:|\n",
"| ✅ |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Install the required dependencies.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%pip install -U langchain-oxylabs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Credentials\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set up the proper API keys and environment variables.\n",
"Create your API user credentials: Sign up for a free trial or purchase the product\n",
"in the [Oxylabs dashboard](https://dashboard.oxylabs.io/en/registration)\n",
"to create your API user credentials (OXYLABS_USERNAME and OXYLABS_PASSWORD)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"os.environ[\"OXYLABS_USERNAME\"] = getpass.getpass(\"Enter your Oxylabs username: \")\n",
"os.environ[\"OXYLABS_PASSWORD\"] = getpass.getpass(\"Enter your Oxylabs password: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialization"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2025-08-06T10:57:51.630011Z",
"start_time": "2025-08-06T10:57:51.623814Z"
}
},
"outputs": [],
"source": [
"from langchain_oxylabs import OxylabsLoader"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2025-08-06T10:57:53.685413Z",
"start_time": "2025-08-06T10:57:53.628859Z"
}
},
"outputs": [],
"source": [
"loader = OxylabsLoader(\n",
" urls=[\n",
" \"https://sandbox.oxylabs.io/products/1\",\n",
" \"https://sandbox.oxylabs.io/products/2\",\n",
" ],\n",
" params={\"markdown\": True},\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Load"
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2025-08-06T10:59:51.487327Z",
"start_time": "2025-08-06T10:59:48.592743Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2751\n",
"[![](data:image/svg+xml...)![logo](data:image/gif;base64...)![logo](/_next/image?url=%2F_next%2Fstatic%2Fmedia%2FnavLogo.a8764883.png&w=750&q=75)](/)\n",
"\n",
"Game platforms:\n",
"\n",
"* **All**\n",
"\n",
"* [Nintendo platform](/products/category/nintendo)\n",
"\n",
"+ wii\n",
"+ wii-u\n",
"+ nintendo-64\n",
"+ switch\n",
"+ gamecube\n",
"+ game-boy-advance\n",
"+ 3ds\n",
"+ ds\n",
"\n",
"* [Xbox platform](/products/category/xbox-platform)\n",
"\n",
"* **Dreamcast**\n",
"\n",
"* [Playstation platform](/products/category/playstation-platform)\n",
"\n",
"* **Pc**\n",
"\n",
"* **Stadia**\n",
"\n",
"Go Back\n",
"\n",
"Note!This is a sandbox website used for web scraping. Information listed in this website does not have any real meaning and should not be associated with the actual products.\n",
"\n",
"![The Legend of Zelda: Ocarina of Time](data:image/gif;base64...)![The Legend of Zelda: Ocarina of Time](/assets/action-adventure.svg)\n",
"\n",
"## The Legend of Zelda: Ocarina of Time\n",
"\n",
"**Developer:** Nintendo**Platform:****Type:** singleplayer\n",
"\n",
"As a young boy, Link is tricked by Ganondorf, the King of the Gerudo Thieves. The evil human uses Link to g\n",
"5542\n",
"[![](data:image/svg+xml...)![logo](data:image/gif;base64...)![logo](/_next/image?url=%2F_next%2Fstatic%2Fmedia%2FnavLogo.a8764883.png&w=750&q=75)](/)\n",
"\n",
"Game platforms:\n",
"\n",
"* **All**\n",
"\n",
"* [Nintendo platform](/products/category/nintendo)\n",
"\n",
"+ wii\n",
"+ wii-u\n",
"+ nintendo-64\n",
"+ switch\n",
"+ gamecube\n",
"+ game-boy-advance\n",
"+ 3ds\n",
"+ ds\n",
"\n",
"* [Xbox platform](/products/category/xbox-platform)\n",
"\n",
"* **Dreamcast**\n",
"\n",
"* [Playstation platform](/products/category/playstation-platform)\n",
"\n",
"* **Pc**\n",
"\n",
"* **Stadia**\n",
"\n",
"Go Back\n",
"\n",
"Note!This is a sandbox website used for web scraping. Information listed in this website does not have any real meaning and should not be associated with the actual products.\n",
"\n",
"![Super Mario Galaxy](data:image/gif;base64...)![Super Mario Galaxy](/assets/action.svg)\n",
"\n",
"## Super Mario Galaxy\n",
"\n",
"**Developer:** Nintendo**Platform:****Type:** singleplayer\n",
"\n",
"[Metacritic's 2007 Wii Game of the Year] The ultimate Nintendo hero is taking the ultimate step ... out into space. Join Mario as he ushers in a new era of video games, de\n"
]
}
],
"source": [
"for document in loader.load():\n",
" print(document.page_content[:1000])"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Lazy Load"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"for document in loader.lazy_load():\n",
" print(document.page_content[:1000])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Advanced examples\n",
"\n",
"The following examples show the usage of `OxylabsLoader` with geolocation, currency, pagination and user agent parameters for Amazon Search and Google Search sources."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"ExecuteTime": {
"end_time": "2025-08-06T11:04:19.901122Z",
"start_time": "2025-08-06T11:04:19.838933Z"
}
},
"outputs": [],
"source": [
"loader = OxylabsLoader(\n",
" queries=[\"gaming headset\", \"gaming chair\", \"computer mouse\"],\n",
" params={\n",
" \"source\": \"amazon_search\",\n",
" \"parse\": True,\n",
" \"geo_location\": \"DE\",\n",
" \"currency\": \"EUR\",\n",
" \"pages\": 3,\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"ExecuteTime": {
"end_time": "2025-08-06T11:07:17.648142Z",
"start_time": "2025-08-06T11:07:17.595629Z"
}
},
"outputs": [],
"source": [
"loader = OxylabsLoader(\n",
" queries=[\"europe gdp per capita\", \"us gdp per capita\"],\n",
" params={\n",
" \"source\": \"google_search\",\n",
" \"parse\": True,\n",
" \"geo_location\": \"Paris, France\",\n",
" \"user_agent_type\": \"mobile\",\n",
" },\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"[More information about this package.](https://github.com/oxylabs/langchain-oxylabs)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -31,7 +31,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install -U langchain-oci"
"!pip install -U oci langchain-community"
]
},
{
@@ -47,7 +47,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_oci.llms import OCIGenAI\n",
"from langchain_community.llms.oci_generative_ai import OCIGenAI\n",
"\n",
"llm = OCIGenAI(\n",
" model_id=\"cohere.command\",\n",

View File

@@ -1,215 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RecallioMemory + LangChain Integration Demo\n",
"A minimal notebook to show drop-in usage of RecallioMemory in LangChain (with scoped writes and recall)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install recallio langchain langchain-recallio openai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup: API Keys & Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_recallio.memory import RecallioMemory\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain.prompts import ChatPromptTemplate\n",
"import os\n",
"\n",
"# Set your keys here or use environment variables\n",
"RECALLIO_API_KEY = os.getenv(\"RECALLIO_API_KEY\", \"YOUR_RECALLIO_API_KEY\")\n",
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\", \"YOUR_OPENAI_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialize RecallioMemory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"memory = RecallioMemory(\n",
" project_id=\"project_abc\",\n",
" api_key=RECALLIO_API_KEY,\n",
" session_id=\"demo-session-001\",\n",
" user_id=\"demo-user-42\",\n",
" default_tags=[\"test\", \"langchain\"],\n",
" return_messages=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build a LangChain ConversationChain with RecallioMemory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# You can swap in any supported LLM here\n",
"llm = ChatOpenAI(api_key=OPENAI_API_KEY, temperature=0)\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"The following is a friendly conversation between a human and an AI. \"\n",
" \"The AI is talkative and provides lots of specific details from its context. \"\n",
" \"If the AI does not know the answer to a question, it truthfully says it does not know.\",\n",
" ),\n",
" (\"placeholder\", \"{history}\"), # RecallioMemory will fill this slot\n",
" (\"human\", \"{input}\"),\n",
" ]\n",
")\n",
"\n",
"# LCEL chain that returns an AIMessage\n",
"base_chain = prompt | llm\n",
"\n",
"\n",
"# Create a stateful chain using RecallioMemory\n",
"def chat_with_memory(user_input: str):\n",
" # Load conversation history from memory\n",
" memory_vars = memory.load_memory_variables({\"input\": user_input})\n",
"\n",
" # Run the chain with history and user input\n",
" response = base_chain.invoke(\n",
" {\"input\": user_input, \"history\": memory_vars.get(\"history\", \"\")}\n",
" )\n",
"\n",
" # Save the conversation to memory\n",
" memory.save_context({\"input\": user_input}, {\"output\": response.content})\n",
"\n",
" return response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example: Chat with Memory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bot: Hello Guillaume! It's nice to meet you. How can I assist you today?\n"
]
}
],
"source": [
"# First user message note the AI remembers the name\n",
"resp1 = chat_with_memory(\"Hi! My name is Guillaume. Remember that.\")\n",
"print(\"Bot:\", resp1.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bot: Your name is Guillaume.\n"
]
}
],
"source": [
"# Second user message AI should recall the name from memory\n",
"resp2 = chat_with_memory(\"What is my name?\")\n",
"print(\"Bot:\", resp2.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## See What Is Stored in Recallio\n",
"This is for debugging/demo only; in production, you wouldn't do this on every run."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current memory variables: {'history': [HumanMessage(content='Name is Guillaume', additional_kwargs={}, response_metadata={})]}\n"
]
}
],
"source": [
"print(\"Current memory variables:\", memory.load_memory_variables({}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clear Memory (Optional Cleanup - Requires Manager level Key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# memory.clear()\n",
"# print(\"Memory cleared.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -2,10 +2,17 @@
This will help you getting started with DigitalOcean Gradient [chat models](/docs/concepts/chat_models).
## Overview
### Integration details
| Class | Package | Package downloads | Package latest |
| :--- | :--- | :---: | :---: |
| [ChatGradient](https://python.langchain.com/api_reference/langchain-gradient/chat_models/langchain_gradient.chat_models.ChatGradient.html) | [langchain-gradient](https://python.langchain.com/api_reference/langchain-gradient/) | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-gradient?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-gradient?style=flat-square&label=%20) |
## Setup
langchain-gradient uses DigitalOcean's Gradient™ AI Platform.
langchain-gradient uses DigitalOcean Gradient Platform.
Create an account on DigitalOcean, acquire a `DIGITALOCEAN_INFERENCE_KEY` API key from the Gradient Platform, and install the `langchain-gradient` integration package.

View File

@@ -11,17 +11,17 @@ The `LangChain` integrations related to [Oracle Cloud Infrastructure](https://ww
To use, you should have the latest `oci` python SDK and the langchain_community package installed.
```bash
pip install -U langchain_oci
pip install -U oci langchain-community
```
See [chat](/docs/integrations/llms/oci_generative_ai), [complete](/docs/integrations/chat/oci_generative_ai), and [embedding](/docs/integrations/text_embedding/oci_generative_ai) usage examples.
```python
from langchain_oci.chat_models import ChatOCIGenAI
from langchain_community.chat_models import ChatOCIGenAI
from langchain_oci.llms import OCIGenAI
from langchain_community.llms import OCIGenAI
from langchain_oci.embeddings import OCIGenAIEmbeddings
from langchain_community.embeddings import OCIGenAIEmbeddings
```
## OCI Data Science Model Deployment Endpoint
@@ -42,8 +42,8 @@ See [chat](/docs/integrations/chat/oci_data_science) and [complete](/docs/integr
```python
from langchain_oci.chat_models import ChatOCIModelDeployment
from langchain_community.chat_models import ChatOCIModelDeployment
from langchain_oci.llms import OCIModelDeploymentLLM
from langchain_community.llms import OCIModelDeploymentLLM
```

View File

@@ -1,31 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Recallio\n",
"\n",
"[Recallio](https://recallio.ai/) is a powerfull API allowing to store, index, and retrieve application “memories” with built-in fact extraction, dynamic summaries, reranked recall, and a full knowledge-graph layer.\n",
"\n",
"\n",
"## Installation\n",
"\n",
"```bash\n",
"pip install langchain-recallio\n",
"```\n",
"\n",
"```python\n",
"from langchain_recallio.memory import RecallioMemory\n",
"```"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -1,43 +0,0 @@
# langchain-siliconflow
This package contains the LangChain integration with SiliconFlow
## Installation
```bash
pip install -U langchain-siliconflow
```
And you should configure credentials by setting the following environment variables:
```bash
export SILICONFLOW_API_KEY="your-api-key"
```
You can set the following environment variable to use the `.cn` endpoint:
```bash
export SILICONFLOW_BASE_URL="https://api.siliconflow.cn/v1"
```
## Chat Models
`ChatSiliconFlow` class exposes chat models from SiliconFlow.
```python
from langchain_siliconflow import ChatSiliconFlow
llm = ChatSiliconFlow()
llm.invoke("Sing a ballad of LangChain.")
```
## Embeddings
`SiliconFlowEmbeddings` class exposes embeddings from SiliconFlow.
```python
from langchain_siliconflow import SiliconFlowEmbeddings
embeddings = SiliconFlowEmbeddings()
embeddings.embed_query("What is the meaning of life?")
```

View File

@@ -1,101 +0,0 @@
# TrueFoundry
TrueFoundry provides an enterprise-ready [AI Gateway](https://www.truefoundry.com/ai-gateway) to provide governance and observability to agentic frameworks like LangChain. TrueFoundry AI Gateway serves as a unified interface for LLM access, providing:
- **Unified API Access**: Connect to 250+ LLMs (OpenAI, Claude, Gemini, Groq, Mistral) through one API
- **Low Latency**: Sub-3ms internal latency with intelligent routing and load balancing
- **Enterprise Security**: SOC 2, HIPAA, GDPR compliance with RBAC and audit logging
- **Quota and cost management**: Token-based quotas, rate limiting, and comprehensive usage tracking
- **Observability**: Full request/response logging, metrics, and traces with customizable retention
## Prerequisites
Before integrating LangChain with TrueFoundry, ensure you have:
1. **TrueFoundry Account**: A [TrueFoundry account](https://www.truefoundry.com/register) with at least one model provider configured. Follow quick start guide [here](https://docs.truefoundry.com/gateway/quick-start)
2. **Personal Access Token**: Generate a token by following the [TrueFoundry token generation guide](https://docs.truefoundry.com/gateway/authentication)
## Quickstart
You can connect to TrueFoundry's unified LLM gateway through the `ChatOpenAI` interface.
- Set the `base_url` to your TrueFoundry endpoint (explained below)
- Set the `api_key` to your TrueFoundry [PAT (Personal Access Token)](https://docs.truefoundry.com/gateway/authentication#personal-access-token-pat)
- Use the same `model-name` as shown in the unified code snippet
![TrueFoundry metrics](/img/unified-code-tfy.png)
### Installation
```bash
pip install langchain-openai
```
### Basic Setup
Connect to TrueFoundry by updating the `ChatOpenAI` model in LangChain:
```python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
api_key=TRUEFOUNDRY_API_KEY,
base_url=TRUEFOUNDRY_GATEWAY_BASE_URL,
model="openai-main/gpt-4o" # Similarly you can call any model from any model provider
)
llm.invoke("What is the meaning of life, universe and everything?")
```
The request is routed through your TrueFoundry gateway to the specified model provider. TrueFoundry automatically handles rate limiting, load balancing, and observability.
### LangGraph Integration
```python
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, MessagesState
from langchain_core.messages import HumanMessage
# Define your LangGraph workflow
def call_model(state: MessagesState):
model = ChatOpenAI(
api_key=TRUEFOUNDRY_API_KEY,
base_url=TRUEFOUNDRY_GATEWAY_BASE_URL,
# Copy the exact model name from gateway
model="openai-main/gpt-4o"
)
response = model.invoke(state["messages"])
return {"messages": [response]}
# Build workflow
workflow = StateGraph(MessagesState)
workflow.add_node("agent", call_model)
workflow.set_entry_point("agent")
workflow.set_finish_point("agent")
app = workflow.compile()
# Run agent through TrueFoundry
result = app.invoke({"messages": [HumanMessage(content="Hello!")]})
```
## Observability and Governance
![TrueFoundry metrics](/img/gateway-metrics.png)
With the Metrics Dashboard, you can monitor and analyze:
- **Performance Metrics**: Track key latency metrics like Request Latency, Time to First Token (TTFS), and Inter-Token Latency (ITL) with P99, P90, and P50 percentiles
- **Cost and Token Usage**: Gain visibility into your application's costs with detailed breakdowns of input/output tokens and the associated expenses for each model
- **Usage Patterns**: Understand how your application is being used with detailed analytics on user activity, model distribution, and team-based usage
- **Rate Limiting & Load Balancing**: Configure limits, distribute traffic across models, and set up fallbacks
## Support
For questions, issues, or support:
- **Email**: [support@truefoundry.com](mailto:support@truefoundry.com)
- **Documentation**: [https://docs.truefoundry.com/](https://docs.truefoundry.com/)

View File

@@ -31,7 +31,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install -U langchain_oci"
"!pip install -U oci"
]
},
{
@@ -71,7 +71,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_oci.embeddings import OCIGenAIEmbeddings\n",
"from langchain_community.embeddings import OCIGenAIEmbeddings\n",
"\n",
"# use default authN method API-key\n",
"embeddings = OCIGenAIEmbeddings(\n",

View File

@@ -153,7 +153,7 @@
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"llm = ChatAnthropic(\n",
" model=\"claude-3-5-sonnet-latest\",\n",
" model=\"claude-3-5-sonnet-20240620\",\n",
")\n",
"\n",
"langgraph_agent_executor = create_react_agent(llm, stripe_agent_toolkit.get_tools())\n",

File diff suppressed because one or more lines are too long

View File

@@ -73,9 +73,8 @@
]
},
{
"cell_type": "markdown",
"id": "72461be913bfaf2b",
"metadata": {},
"cell_type": "markdown",
"source": [
"## Instantiation\n",
"\n",
@@ -84,26 +83,26 @@
"Instantiation\n",
"The tool accepts various parameters during instantiation:\n",
"\n",
"- `max_results` (optional, int): Maximum number of search results to return. Default is 5.\n",
"- `topic` (optional, str): Category of the search. Can be `'general'`, `'news'`, or `'finance'`. Default is `'general'`.\n",
"- `include_answer` (optional, bool): Include an answer to original query in results. Default is False.\n",
"- `include_raw_content` (optional, bool): Include cleaned and parsed HTML of each search result. Default is False.\n",
"- `include_images` (optional, bool): Include a list of query related images in the response. Default is False.\n",
"- `include_image_descriptions` (optional, bool): Include descriptive text for each image. Default is False.\n",
"- `search_depth` (optional, str): Depth of the search, either `'basic'` or `'advanced'`. Default is `'basic'`.\n",
"- `time_range` (optional, str): The time range back from the current date to filter results - `'day'`, `'week'`, `'month'`, or `'year'`. Default is None.\n",
"- `include_domains` (optional, List[str]): List of domains to specifically include. Default is None.\n",
"- `exclude_domains` (optional, List[str]): List of domains to specifically exclude. Default is None.\n",
"- max_results (optional, int): Maximum number of search results to return. Default is 5.\n",
"- topic (optional, str): Category of the search. Can be \"general\", \"news\", or \"finance\". Default is \"general\".\n",
"- include_answer (optional, bool): Include an answer to original query in results. Default is False.\n",
"- include_raw_content (optional, bool): Include cleaned and parsed HTML of each search result. Default is False.\n",
"- include_images (optional, bool): Include a list of query related images in the response. Default is False.\n",
"- include_image_descriptions (optional, bool): Include descriptive text for each image. Default is False.\n",
"- search_depth (optional, str): Depth of the search, either \"basic\" or \"advanced\". Default is \"basic\".\n",
"- time_range (optional, str): The time range back from the current date to filter results - \"day\", \"week\", \"month\", or \"year\". Default is None.\n",
"- include_domains (optional, List[str]): List of domains to specifically include. Default is None.\n",
"- exclude_domains (optional, List[str]): List of domains to specifically exclude. Default is None.\n",
"\n",
"For a comprehensive overview of the available parameters, refer to the [Tavily Search API documentation](https://docs.tavily.com/documentation/api-reference/endpoint/search)"
]
],
"id": "72461be913bfaf2b"
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc382e5426394836",
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"from langchain_tavily import TavilySearch\n",
"\n",
@@ -119,12 +118,12 @@
" # include_domains=None,\n",
" # exclude_domains=None\n",
")"
]
],
"id": "dc382e5426394836"
},
{
"cell_type": "markdown",
"id": "f997d2733b63f655",
"metadata": {},
"cell_type": "markdown",
"source": [
"## Invocation\n",
"\n",
@@ -135,22 +134,18 @@
"- The following arguments can also be set during invocation : `include_images`, `search_depth` , `time_range`, `include_domains`, `exclude_domains`, `include_images`\n",
"- For reliability and performance reasons, certain parameters that affect response size cannot be modified during invocation: `include_answer` and `include_raw_content`. These limitations prevent unexpected context window issues and ensure consistent results.\n",
"\n",
":::note\n",
"\n",
"The optional arguments are available for agents to dynamically set, if you set an argument during instantiation and then invoke the tool with a different value, the tool will use the value you passed during invocation.\n",
"\n",
":::"
]
"NOTE: The optional arguments are available for agents to dynamically set, if you set an argument during instantiation and then invoke the tool with a different value, the tool will use the value you passed during invocation."
],
"id": "f997d2733b63f655"
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e75399230ab9fc1",
"metadata": {},
"cell_type": "code",
"outputs": [],
"source": [
"tool.invoke({\"query\": \"What happened at the last wimbledon\"})"
]
"execution_count": null,
"source": "tool.invoke({\"query\": \"What happened at the last wimbledon\"})",
"id": "5e75399230ab9fc1"
},
{
"cell_type": "markdown",
@@ -159,7 +154,7 @@
"source": [
"### [Invoke with ToolCall](/docs/concepts/tools)\n",
"\n",
"We can also invoke the tool with a model-generated `ToolCall`, in which case a `ToolMessage` will be returned:"
"We can also invoke the tool with a model-generated ToolCall, in which case a ToolMessage will be returned:"
]
},
{
@@ -238,7 +233,7 @@
"id": "1020a506-473b-4e6a-a563-7aaf92c4d183",
"metadata": {},
"source": [
"We will need to install `langgraph`:"
"We will need to install langgraph:"
]
},
{
@@ -261,21 +256,21 @@
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"================================\u001B[1m Human Message \u001B[0m=================================\n",
"\n",
"What nation hosted the Euro 2024? Include only wikipedia sources.\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"==================================\u001B[1m Ai Message \u001B[0m==================================\n",
"Tool Calls:\n",
" tavily_search (call_yxmR4K2uadsQ8LKoyi8JyoLD)\n",
" Call ID: call_yxmR4K2uadsQ8LKoyi8JyoLD\n",
" Args:\n",
" query: Euro 2024 host nation\n",
" include_domains: ['wikipedia.org']\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"=================================\u001B[1m Tool Message \u001B[0m=================================\n",
"Name: tavily_search\n",
"\n",
"{\"query\": \"Euro 2024 host nation\", \"follow_up_questions\": null, \"answer\": null, \"images\": [], \"results\": [{\"title\": \"UEFA Euro 2024 - Wikipedia\", \"url\": \"https://en.wikipedia.org/wiki/UEFA_Euro_2024\", \"content\": \"Tournament details Host country Germany Dates 14 June 14 July Teams 24 Venue(s) 10 (in 10 host cities) Final positions Champions Spain (4th title) Runners-up England Tournament statistics Matches played 51 Goals scored 117 (2.29 per match) Attendance 2,681,288 (52,574 per match) Top scorer(s) Harry Kane Georges Mikautadze Jamal Musiala Cody Gakpo Ivan Schranz Dani Olmo (3 goals each) Best player(s) Rodri Best young player Lamine Yamal ← 2020 2028 → The 2024 UEFA European Football Championship, commonly referred to as UEFA Euro 2024 (stylised as UEFA EURO 2024) or simply Euro 2024, was the 17th UEFA European Championship, the quadrennial international football championship organised by UEFA for the European men's national teams of their member associations. Germany hosted the tournament, which took place from 14 June to 14 July 2024. The tournament involved 24 teams, with Georgia making their European Championship debut. [4] Host nation Germany were eliminated by Spain in the quarter-finals; Spain went on to win the tournament for a record fourth time after defeating England 21 in the final.\", \"score\": 0.9104262, \"raw_content\": null}, {\"title\": \"UEFA Euro 2024 - Simple English Wikipedia, the free encyclopedia\", \"url\": \"https://simple.wikipedia.org/wiki/UEFA_Euro_2024\", \"content\": \"The 2024 UEFA European Football Championship, also known as UEFA Euro 2024 or simply Euro 2024, was the 17th edition of the UEFA European Championship. Germany was hosting the tournament. ... The UEFA Executive Committee voted for the host in a secret ballot, with only a simple majority (more than half of the valid votes) required to determine\", \"score\": 0.81418616, \"raw_content\": null}, {\"title\": \"Championnat d'Europe de football 2024 — Wikipédia\", \"url\": \"https://fr.wikipedia.org/wiki/Championnat_d'Europe_de_football_2024\", \"content\": \"Le Championnat d'Europe de l'UEFA de football 2024 est la 17 e édition du Championnat d'Europe de football, communément abrégé en Euro 2024, compétition organisée par l'UEFA et rassemblant les meilleures équipes nationales masculines européennes. L'Allemagne est désignée pays organisateur de la compétition le 27 septembre 2018. C'est la troisième fois que des matches du Championnat\", \"score\": 0.8055255, \"raw_content\": null}, {\"title\": \"UEFA Euro 2024 bids - Wikipedia\", \"url\": \"https://en.wikipedia.org/wiki/UEFA_Euro_2024_bids\", \"content\": \"The bidding process of UEFA Euro 2024 ended on 27 September 2018 in Nyon, Switzerland, when Germany was announced to be the host. [1] Two bids came before the deadline, 3 March 2017, which were Germany and Turkey as single bids. ... Press agencies revealed on 24 October 2013, that the European football governing body UEFA would have decided on\", \"score\": 0.7882741, \"raw_content\": null}, {\"title\": \"2024 UEFA European Under-19 Championship - Wikipedia\", \"url\": \"https://en.wikipedia.org/wiki/2024_UEFA_European_Under-19_Championship\", \"content\": \"The 2024 UEFA European Under-19 Championship (also known as UEFA Under-19 Euro 2024) was the 21st edition of the UEFA European Under-19 Championship (71st edition if the Under-18 and Junior eras are included), the annual international youth football championship organised by UEFA for the men's under-19 national teams of Europe. Northern Ireland hosted the tournament from 15 to 28 July 2024.\", \"score\": 0.7783298, \"raw_content\": null}], \"response_time\": 1.67}\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"==================================\u001B[1m Ai Message \u001B[0m==================================\n",
"\n",
"The nation that hosted Euro 2024 was Germany. You can find more information on the [Wikipedia page for UEFA Euro 2024](https://en.wikipedia.org/wiki/UEFA_Euro_2024).\n"
]
@@ -309,14 +304,8 @@
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all Tavily Search API features and configurations head to the [API reference](https://docs.tavily.com/documentation/api-reference/endpoint/search)."
"For detailed documentation of all Tavily Search API features and configurations head to the API reference: https://docs.tavily.com/documentation/api-reference/endpoint/search"
]
},
{
"cell_type": "markdown",
"id": "589ff839",
"metadata": {},
"source": []
}
],
"metadata": {

View File

@@ -9,7 +9,7 @@
"\n",
"This notebook covers how to get started with the `Chroma` vector store.\n",
"\n",
">[Chroma](https://docs.trychroma.com/getting-started) is a AI-native open-source vector database focused on developer productivity and happiness. Chroma is licensed under Apache 2.0. View the full docs of `Chroma` at [this page](https://docs.trychroma.com/integrations/frameworks/langchain), and find the API reference for the LangChain integration at [this page](https://python.langchain.com/api_reference/chroma/vectorstores/langchain_chroma.vectorstores.Chroma.html).\n",
">[Chroma](https://docs.trychroma.com/getting-started) is a AI-native open-source vector database focused on developer productivity and happiness. Chroma is licensed under Apache 2.0. View the full docs of `Chroma` at [this page](https://docs.trychroma.com/reference/py-collection), and find the API reference for the LangChain integration at [this page](https://python.langchain.com/api_reference/chroma/vectorstores/langchain_chroma.vectorstores.Chroma.html).\n",
"\n",
":::info Chroma Cloud\n",
"\n",
@@ -522,39 +522,6 @@
"vector_store.delete(ids=uuids[-1])"
]
},
{
"cell_type": "markdown",
"id": "675b3708-b5ef-4298-b950-eac27096b456",
"metadata": {},
"source": [
"### Fork a vector store\n",
"\n",
"Forking lets you create a new `Chroma` vector store from an existing one instantly, using copy-on-write under the hood. This means that your new `Chroma` store is identical to the origin, but any modifications to it will not affect the origin, and vice-versa.\n",
"\n",
"Forks are great for any use case that benefits from data versioning. You can learn more about forking in the [Chroma docs](https://docs.trychroma.com/cloud/collection-forking).\n",
"\n",
"Note: Forking is only avaiable on `Chroma` instances with a Chroma Cloud connection."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e08a0c79-4d2a-49ff-be63-d8591c268764",
"metadata": {},
"outputs": [],
"source": [
"forked_store = vector_store.fork(new_name=\"my_forked_collection\")\n",
"\n",
"updated_document_2 = Document(\n",
" page_content=\"The weather forecast for tomorrow is extrmeley hot, with a high of 100 degrees.\",\n",
" metadata={\"source\": \"news\"},\n",
" id=2,\n",
")\n",
"\n",
"# Update does not affect 'vector_store'\n",
"forked_store.update(ids=[\"2\"], documents=[updated_document_2])"
]
},
{
"cell_type": "markdown",
"id": "213acf08",
@@ -642,7 +609,7 @@
"source": [
"#### Other search methods\n",
"\n",
"There are a variety of other search methods that are not covered in this notebook. For a full list of the search abilities available for `Chroma` check out the [API reference](https://python.langchain.com/api_reference/chroma/vectorstores/langchain_chroma.vectorstores.Chroma.html).\n",
"There are a variety of other search methods that are not covered in this notebook, such as MMR search or searching by vector. For a full list of the search abilities available for `AstraDBVectorStore` check out the [API reference](https://python.langchain.com/api_reference/astradb/vectorstores/langchain_astradb.vectorstores.AstraDBVectorStore.html).\n",
"\n",
"### Query by turning into retriever\n",
"\n",
@@ -703,7 +670,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
"version": "3.12.0"
}
},
"nbformat": 4,

View File

@@ -11,7 +11,7 @@ LangChain simplifies every stage of the LLM application lifecycle:
- **Development**: Build your applications using LangChain's open-source [components](/docs/concepts) and [third-party integrations](/docs/integrations/providers/).
Use [LangGraph](/docs/concepts/architecture/#langgraph) to build stateful agents with first-class streaming and human-in-the-loop support.
- **Productionization**: Use [LangSmith](https://docs.smith.langchain.com/) to inspect, monitor and evaluate your applications, so that you can continuously optimize and deploy with confidence.
- **Deployment**: Turn your LangGraph applications into production-ready APIs and Assistants with [LangGraph Platform](https://docs.langchain.com/langgraph-platform).
- **Deployment**: Turn your LangGraph applications into production-ready APIs and Assistants with [LangGraph Platform](https://langchain-ai.github.io/langgraph/cloud/).
import ThemedImage from '@theme/ThemedImage';
import useBaseUrl from '@docusaurus/useBaseUrl';

View File

@@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -74,7 +74,7 @@
"\n",
"uncoercible_message = {\"role\": \"HumanMessage\", \"random_field\": \"random value\"}\n",
"\n",
"model = ChatAnthropic(model=\"claude-3-5-sonnet-latest\")\n",
"model = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\")\n",
"\n",
"model.invoke([uncoercible_message])"
]

View File

@@ -720,7 +720,7 @@
" AIMessage(content='yes!', additional_kwargs={}, response_metadata={})]"
]
},
"execution_count": 109,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -771,13 +771,8 @@
"\n",
"\n",
"def call_model(state: State):\n",
" print(f\"Messages before trimming: {len(state['messages'])}\")\n",
" # highlight-start\n",
" trimmed_messages = trimmer.invoke(state[\"messages\"])\n",
" print(f\"Messages after trimming: {len(trimmed_messages)}\")\n",
" print(\"Remaining messages:\")\n",
" for msg in trimmed_messages:\n",
" print(f\" {type(msg).__name__}: {msg.content}\")\n",
" prompt = prompt_template.invoke(\n",
" {\"messages\": trimmed_messages, \"language\": state[\"language\"]}\n",
" )\n",
@@ -797,7 +792,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now if we try asking the model our name, it won't know it since we trimmed that part of the chat history. (By defining our trim stragegy as `'last'`, we are only keeping the most recent messages that fit within the `max_tokens`.)"
"Now if we try asking the model our name, it won't know it since we trimmed that part of the chat history:"
]
},
{
@@ -809,20 +804,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Messages before trimming: 12\n",
"Messages after trimming: 8\n",
"Remaining messages:\n",
" SystemMessage: you're a good assistant\n",
" HumanMessage: whats 2 + 2\n",
" AIMessage: 4\n",
" HumanMessage: thanks\n",
" AIMessage: no problem!\n",
" HumanMessage: having fun?\n",
" AIMessage: yes!\n",
" HumanMessage: What is my name?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"I don't know your name. If you'd like to share it, feel free!\n"
"I don't know your name. You haven't told me yet!\n"
]
}
],
@@ -856,27 +840,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Messages before trimming: 12\n",
"Messages after trimming: 8\n",
"Remaining messages:\n",
" SystemMessage: you're a good assistant\n",
" HumanMessage: whats 2 + 2\n",
" AIMessage: 4\n",
" HumanMessage: thanks\n",
" AIMessage: no problem!\n",
" HumanMessage: having fun?\n",
" AIMessage: yes!\n",
" HumanMessage: What math problem was asked?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The math problem that was asked was \"what's 2 + 2.\"\n"
"You asked what 2 + 2 equals.\n"
]
}
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"abc678\"}}\n",
"\n",
"query = \"What math problem was asked?\"\n",
"query = \"What math problem did I ask?\"\n",
"language = \"English\"\n",
"\n",
"input_messages = messages + [HumanMessage(query)]\n",
@@ -918,9 +890,9 @@
"text": [
"|Hi| Todd|!| Here|s| a| joke| for| you|:\n",
"\n",
"|Why| don't| scientists| trust| atoms|?\n",
"|Why| don|t| skeleton|s| fight| each| other|?\n",
"\n",
"|Because| they| make| up| everything|!||"
"|Because| they| don|t| have| the| guts|!||"
]
}
],

View File

@@ -49,7 +49,7 @@
"metadata": {},
"outputs": [],
"source": [
"pip install -U langchain-core"
"pip install --upgrade --quiet langchain-core"
]
},
{
@@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "39f3ce3e",
"metadata": {},
"outputs": [],
@@ -124,7 +124,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "5509b6a6",
"metadata": {},
"outputs": [
@@ -134,7 +134,7 @@
"Classification(sentiment='positive', aggressiveness=1, language='Spanish')"
]
},
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -157,17 +157,17 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "9154474c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'sentiment': 'angry', 'aggressiveness': 8, 'language': 'Spanish'}"
"{'sentiment': 'enojado', 'aggressiveness': 8, 'language': 'es'}"
]
},
"execution_count": 4,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -218,7 +218,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 11,
"id": "6a5f7961",
"metadata": {},
"outputs": [],
@@ -237,7 +237,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 15,
"id": "e5a5881f",
"metadata": {},
"outputs": [],
@@ -268,17 +268,17 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 12,
"id": "d9b9d53d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Classification(sentiment='happy', aggressiveness=1, language='spanish')"
"Classification(sentiment='positive', aggressiveness=1, language='Spanish')"
]
},
"execution_count": 7,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -291,17 +291,17 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 13,
"id": "1c12fa00",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Classification(sentiment='sad', aggressiveness=4, language='spanish')"
"Classification(sentiment='enojado', aggressiveness=8, language='es')"
]
},
"execution_count": 8,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -314,17 +314,17 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 14,
"id": "0bdfcb05",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Classification(sentiment='happy', aggressiveness=1, language='english')"
"Classification(sentiment='neutral', aggressiveness=1, language='English')"
]
},
"execution_count": 9,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -359,7 +359,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "langchain-monorepo",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -373,7 +373,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.11"
"version": "3.10.4"
}
},
"nbformat": 4,

View File

@@ -192,7 +192,7 @@
"source": [
":::tip\n",
"\n",
"If we've enabled LangSmith, we can see that this run is logged to LangSmith, and can see the [LangSmith trace](https://docs.smith.langchain.com/observability/concepts#traces). The LangSmith trace reports [token](/docs/concepts/tokens/) usage information, latency, [standard model parameters](/docs/concepts/chat_models/#standard-parameters) (such as temperature), and other information.\n",
"If we've enabled LangSmith, we can see that this run is logged to LangSmith, and can see the [LangSmith trace](https://smith.langchain.com/public/88baa0b2-7c1a-4d09-ba30-a47985dde2ea/r). The LangSmith trace reports [token](/docs/concepts/tokens/) usage information, latency, [standard model parameters](/docs/concepts/chat_models/#standard-parameters) (such as temperature), and other information.\n",
"\n",
":::\n",
"\n",

View File

@@ -236,7 +236,7 @@
"We can use [create_stuff_documents_chain](https://python.langchain.com/api_reference/langchain/chains/langchain.chains.combine_documents.stuff.create_stuff_documents_chain.html), especially if using larger context window models such as:\n",
"\n",
"* 128k token OpenAI `gpt-4o` \n",
"* 200k token Anthropic `claude-3-5-sonnet-latest`\n",
"* 200k token Anthropic `claude-3-5-sonnet-20240620`\n",
"\n",
"The chain will take a list of documents, insert them all into a prompt, and pass that prompt to an LLM:"
]

View File

@@ -142,7 +142,8 @@ const config = {
respectPrefersColorScheme: true,
},
announcementBar: {
content: "Our new LangChain Academy Course Deep Research with LangGraph is now live! <a href='https://academy.langchain.com/courses/deep-research-with-langgraph/?utm_medium=internal&utm_source=docs&utm_campaign=q3-2025_deep-research-course_co' target='_blank'>Enroll for free</a>.",
content:
'<strong>Our <a href="https://academy.langchain.com/courses/ambient-agents/?utm_medium=internal&utm_source=docs&utm_campaign=q2-2025_ambient-agents_co" target="_blank">Building Ambient Agents with LangGraph</a> course is now available on LangChain Academy!</strong>',
backgroundColor: "#d0c9fe",
},
prism: {

View File

@@ -5,14 +5,6 @@ echo "VERCEL_GIT_COMMIT_REF: $VERCEL_GIT_COMMIT_REF"
echo "VERCEL_GIT_REPO_OWNER: $VERCEL_GIT_REPO_OWNER"
echo "VERCEL_GIT_REPO_SLUG: $VERCEL_GIT_REPO_SLUG"
echo "Checking for skip-preview tags..."
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
echo "Commit message: $COMMIT_MESSAGE"
if [[ "$COMMIT_MESSAGE" == *"[skip-preview]"* ]] || [[ "$COMMIT_MESSAGE" == *"[no-preview]"* ]] || [[ "$COMMIT_MESSAGE" == *"[skip-deploy]"* ]]; then
echo "🛑 Skip-preview tag found in commit message - skipping preview deployment"
exit 0
fi
if { \
[ "$VERCEL_ENV" == "production" ] || \
@@ -21,10 +13,10 @@ if { \
[ "$VERCEL_GIT_COMMIT_REF" == "v0.2" ] || \
[ "$VERCEL_GIT_COMMIT_REF" == "v0.3rc" ]; \
} && [ "$VERCEL_GIT_REPO_OWNER" == "langchain-ai" ]
then
then
echo "✅ Production build - proceeding with build"
exit 1
fi
fi
echo "Checking for changes in docs/"

View File

@@ -27,7 +27,7 @@ module.exports = {
},
{
type: "category",
link: { type: 'doc', id: 'tutorials/index' },
link: {type: 'doc', id: 'tutorials/index'},
label: "Tutorials",
collapsible: false,
items: [{
@@ -38,7 +38,7 @@ module.exports = {
},
{
type: "category",
link: { type: 'doc', id: 'how_to/index' },
link: {type: 'doc', id: 'how_to/index'},
label: "How-to guides",
collapsible: false,
items: [{
@@ -49,7 +49,7 @@ module.exports = {
},
{
type: "category",
link: { type: 'doc', id: 'concepts/index' },
link: {type: 'doc', id: 'concepts/index'},
label: "Conceptual guide",
collapsible: false,
items: [{
@@ -103,7 +103,7 @@ module.exports = {
{
type: "category",
label: "Migrating from v0.0 chains",
link: { type: 'doc', id: 'versions/migrating_chains/index' },
link: {type: 'doc', id: 'versions/migrating_chains/index'},
collapsible: false,
collapsed: false,
items: [{
@@ -115,7 +115,7 @@ module.exports = {
{
type: "category",
label: "Upgrading to LangGraph memory",
link: { type: 'doc', id: 'versions/migrating_memory/index' },
link: {type: 'doc', id: 'versions/migrating_memory/index'},
collapsible: false,
collapsed: false,
items: [{
@@ -418,7 +418,7 @@ module.exports = {
},
],
},
],
link: {
type: "generated-index",
@@ -434,7 +434,7 @@ module.exports = {
},
{
type: "category",
link: { type: 'doc', id: 'contributing/tutorials/index' },
link: {type: 'doc', id: 'contributing/tutorials/index'},
label: "Tutorials",
collapsible: false,
items: [{
@@ -445,7 +445,7 @@ module.exports = {
},
{
type: "category",
link: { type: 'doc', id: 'contributing/how_to/index' },
link: {type: 'doc', id: 'contributing/how_to/index'},
label: "How-to guides",
collapsible: false,
items: [{
@@ -456,7 +456,7 @@ module.exports = {
},
{
type: "category",
link: { type: 'doc', id: 'contributing/reference/index' },
link: {type: 'doc', id: 'contributing/reference/index'},
label: "Reference & FAQ",
collapsible: false,
items: [{

View File

@@ -856,13 +856,6 @@ const FEATURE_TABLES = {
source: "Web interaction and structured data extraction from any web page using an AgentQL query or a Natural Language prompt",
api: "API",
apiLink: "https://python.langchain.com/docs/integrations/document_loaders/agentql/"
},
{
name: "Oxylabs",
link: "oxylabs",
source: "Web intelligence platform enabling the access to various data sources.",
api: "API",
apiLink: "https://github.com/oxylabs/langchain-oxylabs"
}
]
},

Binary file not shown.

Before

Width:  |  Height:  |  Size: 530 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 408 KiB

View File

@@ -11,5 +11,3 @@ numpy>=1.26.0,<2.0.0
simsimd>=5.0.0
# Fix sentencepiece build error - use newer version that supports modern CMake
sentencepiece>=0.2.1
# Fix langchain-azure-ai dependency conflict with langchain-core
langchain-core @ file:///home/runner/work/langchain/langchain/langchain/libs/core

View File

@@ -1,5 +1,3 @@
"""LangChain CLI."""
from langchain_cli._version import __version__
__all__ = [

View File

@@ -1,5 +1,3 @@
"""LangChain CLI."""
from typing import Annotated, Optional
import typer
@@ -36,21 +34,20 @@ app.command(
)
def _version_callback(*, show_version: bool) -> None:
def version_callback(show_version: bool) -> None: # noqa: FBT001
if show_version:
typer.echo(f"langchain-cli {__version__}")
raise typer.Exit
@app.callback()
def _main(
*,
version: bool = typer.Option(
def main(
version: bool = typer.Option( # noqa: FBT001
False, # noqa: FBT003
"--version",
"-v",
help="Print the current CLI version.",
callback=_version_callback,
callback=version_callback,
is_eager=True,
),
) -> None:

View File

@@ -1,5 +1,3 @@
"""LangChain CLI constants."""
DEFAULT_GIT_REPO = "https://github.com/langchain-ai/langchain.git"
DEFAULT_GIT_SUBDIRECTORY = "templates"
DEFAULT_GIT_REF = "master"

View File

@@ -13,7 +13,7 @@ def create_demo_server(
*,
config_keys: Sequence[str] = (),
playground_type: Literal["default", "chat"] = "default",
) -> FastAPI:
):
"""Create a demo server for the current template."""
app = FastAPI()
package_root = get_package_root()
@@ -40,11 +40,9 @@ def create_demo_server(
return app
def create_demo_server_configurable() -> FastAPI:
"""Create a configurable demo server."""
def create_demo_server_configurable():
return create_demo_server(config_keys=["configurable"])
def create_demo_server_chat() -> FastAPI:
"""Create a chat demo server."""
def create_demo_server_chat():
return create_demo_server(playground_type="chat")

View File

@@ -1 +0,0 @@
"""Namespaces."""

View File

@@ -8,7 +8,6 @@ from pathlib import Path
from typing import Annotated, Optional
import typer
import uvicorn
from langchain_cli.utils.events import create_events
from langchain_cli.utils.git import (
@@ -262,7 +261,7 @@ def add(
cmd = ["pip", "install", "-e", *installed_destination_strs]
cmd_str = " \\\n ".join(installed_destination_strs)
typer.echo(f"Running: pip install -e \\\n {cmd_str}")
subprocess.run(cmd, cwd=cwd, check=True) # noqa: S603
subprocess.run(cmd, cwd=cwd) # noqa: S603
chain_names = []
for e in installed_exports:
@@ -368,6 +367,8 @@ def serve(
app_str = app if app is not None else "app.server:app"
host_str = host if host is not None else "127.0.0.1"
import uvicorn
uvicorn.run(
app_str,
host=host_str,

View File

@@ -15,8 +15,6 @@ integration_cli = typer.Typer(no_args_is_help=True, add_completion=False)
class Replacements(TypedDict):
"""Replacements."""
__package_name__: str
__module_name__: str
__ModuleName__: str
@@ -129,7 +127,6 @@ def new(
subprocess.run(
["poetry", "install", "--with", "lint,test,typing,test_integration"], # noqa: S607
cwd=destination_dir,
check=True,
)
else:
# confirm src and dst are the same length

View File

@@ -1 +0,0 @@
"""Migrations."""

View File

@@ -1 +0,0 @@
"""Generate migrations."""

View File

@@ -3,7 +3,6 @@
import importlib
import inspect
import pkgutil
from types import ModuleType
def generate_raw_migrations(
@@ -90,7 +89,7 @@ def generate_top_level_imports(pkg: str) -> list[tuple[str, str]]:
items = []
# Function to handle importing from modules
def handle_module(module: ModuleType, module_name: str) -> None:
def handle_module(module, module_name) -> None:
if hasattr(module, "__all__"):
all_objects = module.__all__
for name in all_objects:

View File

@@ -1,6 +1,3 @@
"""Migration as Grit file."""
def split_package(package: str) -> tuple[str, str]:
"""Split a package name into the containing package and the final name."""
parts = package.split(".")

View File

@@ -1,11 +1,8 @@
"""Generate migrations utilities."""
import ast
import inspect
import os
import pathlib
from pathlib import Path
from types import ModuleType
from typing import Any, Optional
HERE = Path(__file__).parent
@@ -18,14 +15,12 @@ PARTNER_PKGS = PKGS_ROOT / "partners"
class ImportExtractor(ast.NodeVisitor):
"""Import extractor."""
def __init__(self, *, from_package: Optional[str] = None) -> None:
"""Extract all imports from the given code, optionally filtering by package."""
self.imports: list = []
self.package = from_package
def visit_ImportFrom(self, node: ast.ImportFrom) -> None: # noqa: N802
def visit_ImportFrom(self, node) -> None: # noqa: N802
if node.module and (
self.package is None or str(node.module).startswith(self.package)
):
@@ -44,7 +39,7 @@ def _get_class_names(code: str) -> list[str]:
# Define a node visitor class to collect class names
class ClassVisitor(ast.NodeVisitor):
def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
def visit_ClassDef(self, node) -> None: # noqa: N802
class_names.append(node.name)
self.generic_visit(node)
@@ -63,7 +58,7 @@ def is_subclass(class_obj: Any, classes_: list[type]) -> bool:
)
def find_subclasses_in_module(module: ModuleType, classes_: list[type]) -> list[str]:
def find_subclasses_in_module(module, classes_: list[type]) -> list[str]:
"""Find all classes in the module that inherit from one of the classes."""
subclasses = []
# Iterate over all attributes of the module that are classes
@@ -75,7 +70,8 @@ def find_subclasses_in_module(module: ModuleType, classes_: list[type]) -> list[
def _get_all_classnames_from_file(file: Path, pkg: str) -> list[tuple[str, str]]:
"""Extract all class names from a file."""
code = Path(file).read_text(encoding="utf-8")
with open(file, encoding="utf-8") as f:
code = f.read()
module_name = _get_current_module(file, pkg)
class_names = _get_class_names(code)
@@ -88,7 +84,8 @@ def identify_all_imports_in_file(
from_package: Optional[str] = None,
) -> list[tuple[str, str]]:
"""Let's also identify all the imports in the given file."""
code = Path(file).read_text(encoding="utf-8")
with open(file, encoding="utf-8") as f:
code = f.read()
return find_imports_from_package(code, from_package=from_package)
@@ -146,7 +143,6 @@ def find_imports_from_package(
*,
from_package: Optional[str] = None,
) -> list[tuple[str, str]]:
"""Find imports in code."""
# Parse the code into an AST
tree = ast.parse(code)
# Create an instance of the visitor
@@ -158,7 +154,8 @@ def find_imports_from_package(
def _get_current_module(path: Path, pkg_root: str) -> str:
"""Convert a path to a module name."""
relative_path = path.relative_to(pkg_root).with_suffix("")
path_as_pathlib = pathlib.Path(os.path.abspath(path))
relative_path = path_as_pathlib.relative_to(pkg_root).with_suffix("")
posix_path = relative_path.as_posix()
norm_path = os.path.normpath(str(posix_path))
fully_qualified_module = norm_path.replace("/", ".")

View File

@@ -7,9 +7,7 @@ from pathlib import Path
from typing import Annotated, Optional
import typer
import uvicorn
from langchain_cli.utils.github import list_packages
from langchain_cli.utils.packages import get_langserve_export, get_package_root
package_cli = typer.Typer(no_args_is_help=True, add_completion=False)
@@ -81,7 +79,7 @@ def new(
# poetry install
if with_poetry:
subprocess.run(["poetry", "install"], cwd=destination_dir, check=True) # noqa: S607
subprocess.run(["poetry", "install"], cwd=destination_dir) # noqa: S607
@package_cli.command()
@@ -130,6 +128,8 @@ def serve(
)
)
import uvicorn
uvicorn.run(
script,
factory=True,
@@ -142,6 +142,8 @@ def serve(
@package_cli.command()
def list(contains: Annotated[Optional[str], typer.Argument()] = None) -> None: # noqa: A001
"""List all or search for available templates."""
from langchain_cli.utils.github import list_packages
packages = list_packages(contains=contains)
for package in packages:
typer.echo(package)

View File

@@ -1 +0,0 @@
"""Utilities."""

View File

@@ -1,5 +1,3 @@
"""Events utilities."""
import http.client
import json
from typing import Any, Optional, TypedDict
@@ -10,19 +8,11 @@ WRITE_KEY = "310apTK0HUFl4AOv"
class EventDict(TypedDict):
"""Event data structure for analytics tracking.
Attributes:
event: The name of the event.
properties: Optional dictionary of event properties.
"""
event: str
properties: Optional[dict[str, Any]]
def create_events(events: list[EventDict]) -> Optional[Any]:
"""Create events."""
try:
data = {
"events": [

View File

@@ -1,10 +1,7 @@
"""Find and replace text in files."""
from pathlib import Path
def find_and_replace(source: str, replacements: dict[str, str]) -> str:
"""Find and replace text in a string."""
rtn = source
# replace keys in deterministic alphabetical order
@@ -16,7 +13,6 @@ def find_and_replace(source: str, replacements: dict[str, str]) -> str:
def replace_file(source: Path, replacements: dict[str, str]) -> None:
"""Replace text in a file."""
try:
content = source.read_text()
except UnicodeDecodeError:
@@ -28,7 +24,6 @@ def replace_file(source: Path, replacements: dict[str, str]) -> None:
def replace_glob(parent: Path, glob: str, replacements: dict[str, str]) -> None:
"""Replace text in files matching a glob pattern."""
for file in parent.glob(glob):
if not file.is_file():
continue

View File

@@ -1,7 +1,4 @@
"""Git utilities."""
import hashlib
import logging
import re
import shutil
from collections.abc import Sequence
@@ -16,12 +13,8 @@ from langchain_cli.constants import (
DEFAULT_GIT_SUBDIRECTORY,
)
logger = logging.getLogger(__name__)
class DependencySource(TypedDict):
"""Dependency source information."""
git: str
ref: Optional[str]
subdirectory: Optional[str]
@@ -36,7 +29,6 @@ def parse_dependency_string(
branch: Optional[str],
api_path: Optional[str],
) -> DependencySource:
"""Parse a dependency string into a DependencySource."""
if dep is not None and dep.startswith("git+"):
if repo is not None or branch is not None:
msg = (
@@ -129,7 +121,6 @@ def parse_dependencies(
branch: list[str],
api_path: list[str],
) -> list[DependencySource]:
"""Parse dependencies."""
num_deps = max(
len(dependencies) if dependencies is not None else 0,
len(repo),
@@ -177,22 +168,22 @@ def _get_repo_path(gitstring: str, ref: Optional[str], repo_dir: Path) -> Path:
def update_repo(gitstring: str, ref: Optional[str], repo_dir: Path) -> Path:
"""Update a git repository to the specified ref."""
# see if path already saved
repo_path = _get_repo_path(gitstring, ref, repo_dir)
if repo_path.exists():
# try pulling
try:
repo = Repo(repo_path)
if repo.active_branch.name == ref:
repo.remotes.origin.pull()
return repo_path
if repo.active_branch.name != ref:
raise ValueError
repo.remotes.origin.pull()
except Exception:
logger.exception("Failed to pull existing repo")
# if it fails, delete and clone again
shutil.rmtree(repo_path)
# if it fails, delete and clone again
shutil.rmtree(repo_path)
Repo.clone_from(gitstring, repo_path, branch=ref, depth=1)
else:
Repo.clone_from(gitstring, repo_path, branch=ref, depth=1)
Repo.clone_from(gitstring, repo_path, branch=ref, depth=1)
return repo_path
@@ -205,7 +196,7 @@ def copy_repo(
Raises FileNotFound error if it can't find source
"""
def ignore_func(_: str, files: list[str]) -> list[str]:
def ignore_func(_, files):
return [f for f in files if f == ".git"]
shutil.copytree(source, destination, ignore=ignore_func)

View File

@@ -1,12 +1,9 @@
"""GitHub utilities."""
import http.client
import json
from typing import Optional
def list_packages(*, contains: Optional[str] = None) -> list[str]:
"""List all packages in the langchain repository templates directory."""
conn = http.client.HTTPSConnection("api.github.com")
try:
headers = {

View File

@@ -1,5 +1,3 @@
"""Packages utilities."""
from pathlib import Path
from typing import Any, Optional, TypedDict
@@ -7,7 +5,6 @@ from tomlkit import load
def get_package_root(cwd: Optional[Path] = None) -> Path:
"""Get package root directory."""
# traverse path for routes to host (any directory holding a pyproject.toml file)
package_root = Path.cwd() if cwd is None else cwd
visited: set[Path] = set()
@@ -38,8 +35,7 @@ class LangServeExport(TypedDict):
def get_langserve_export(filepath: Path) -> LangServeExport:
"""Get LangServe export information from a pyproject.toml file."""
with filepath.open() as f:
with open(filepath) as f:
data: dict[str, Any] = load(f)
try:
module = data["tool"]["langserve"]["export_module"]

View File

@@ -1,5 +1,3 @@
"""Pyproject.toml utilities."""
import contextlib
from collections.abc import Iterable
from pathlib import Path
@@ -20,7 +18,7 @@ def add_dependencies_to_pyproject_toml(
local_editable_dependencies: Iterable[tuple[str, Path]],
) -> None:
"""Add dependencies to pyproject.toml."""
with pyproject_toml.open(encoding="utf-8") as f:
with open(pyproject_toml, encoding="utf-8") as f:
# tomlkit types aren't amazing - treat as Dict instead
pyproject: dict[str, Any] = load(f)
pyproject["tool"]["poetry"]["dependencies"].update(
@@ -29,7 +27,7 @@ def add_dependencies_to_pyproject_toml(
for name, loc in local_editable_dependencies
},
)
with pyproject_toml.open("w", encoding="utf-8") as f:
with open(pyproject_toml, "w", encoding="utf-8") as f:
dump(pyproject, f)
@@ -38,13 +36,12 @@ def remove_dependencies_from_pyproject_toml(
local_editable_dependencies: Iterable[str],
) -> None:
"""Remove dependencies from pyproject.toml."""
with pyproject_toml.open(encoding="utf-8") as f:
with open(pyproject_toml, encoding="utf-8") as f:
pyproject: dict[str, Any] = load(f)
# tomlkit types aren't amazing - treat as Dict instead
dependencies = pyproject["tool"]["poetry"]["dependencies"]
for name in local_editable_dependencies:
with contextlib.suppress(KeyError):
del dependencies[name]
with pyproject_toml.open("w", encoding="utf-8") as f:
with open(pyproject_toml, "w", encoding="utf-8") as f:
dump(pyproject, f)

View File

@@ -48,41 +48,58 @@ exclude = [
]
[tool.ruff.lint]
select = [ "ALL",]
select = [
"A", # flake8-builtins
"B", # flake8-bugbear
"ARG", # flake8-unused-arguments
"ASYNC", # flake8-async
"C4", # flake8-comprehensions
"COM", # flake8-commas
"D", # pydocstyle
"E", # pycodestyle error
"EM", # flake8-errmsg
"F", # pyflakes
"FA", # flake8-future-annotations
"FBT", # flake8-boolean-trap
"FLY", # flake8-flynt
"I", # isort
"ICN", # flake8-import-conventions
"INT", # flake8-gettext
"ISC", # isort-comprehensions
"N", # pep8-naming
"PT", # flake8-pytest-style
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PERF", # flake8-perf
"PYI", # flake8-pyi
"Q", # flake8-quotes
"RET", # flake8-return
"RSE", # flake8-rst-docstrings
"RUF", # ruff
"S", # flake8-bandit
"SLF", # flake8-self
"SLOT", # flake8-slots
"SIM", # flake8-simplify
"T10", # flake8-debugger
"T20", # flake8-print
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"W", # pycodestyle warning
"YTT", # flake8-2020
]
ignore = [
"C90", # McCabe complexity
"D100", # pydocstyle: Missing docstring in public module
"D101", # pydocstyle: Missing docstring in public class
"D102", # pydocstyle: Missing docstring in public method
"D103", # pydocstyle: Missing docstring in public function
"D104", # pydocstyle: Missing docstring in public package
"D105", # pydocstyle: Missing docstring in magic method
"D107", # pydocstyle: Missing docstring in __init__
"D407", # pydocstyle: Missing-dashed-underline-after-section
"COM812", # Messes with the formatter
"FIX002", # Line contains TODO
"PERF203", # Rarely useful
"PLR09", # Too many something (arg, statements, etc)
"RUF012", # Doesn't play well with Pydantic
"TC001", # Doesn't play well with Pydantic
"TC002", # Doesn't play well with Pydantic
"TC003", # Doesn't play well with Pydantic
"TD002", # Missing author in TODO
"TD003", # Missing issue link in TODO
# TODO rules
"ANN401",
"BLE",
"D1",
]
unfixable = [
"B028", # People should intentionally tune the stacklevel
"PLW1510", # People should intentionally set the check argument
]
flake8-annotations.allow-star-arg-any = true
flake8-annotations.mypy-init-return = true
flake8-type-checking.runtime-evaluated-base-classes = ["pydantic.BaseModel","langchain_core.load.serializable.Serializable","langchain_core.runnables.base.RunnableSerializable"]
pep8-naming.classmethod-decorators = [ "classmethod", "langchain_core.utils.pydantic.pre_init", "pydantic.field_validator", "pydantic.v1.root_validator",]
pydocstyle.convention = "google"
pyupgrade.keep-runtime-typing = true
[tool.ruff.lint.per-file-ignores]
"tests/**" = [ "D1", "S", "SLF",]
"scripts/**" = [ "INP", "S",]
[tool.mypy]
exclude = [
"langchain_cli/integration_template",

View File

@@ -1 +0,0 @@
"""Scripts."""

View File

@@ -1,8 +1,8 @@
"""Script to generate migrations for the migration script."""
import json
import os
import pkgutil
from pathlib import Path
from typing import Optional
import click
@@ -73,18 +73,19 @@ def generic(
else:
dumped = dump_migrations_as_grit(name, migrations)
Path(output).write_text(dumped)
with open(output, "w") as f:
f.write(dumped)
def handle_partner(pkg: str, output: Optional[str] = None) -> None:
"""Handle partner package migrations."""
migrations = get_migrations_for_partner_package(pkg)
# Run with python 3.9+
name = pkg.removeprefix("langchain_")
data = dump_migrations_as_grit(name, migrations)
output_name = f"{name}.grit" if output is None else output
if migrations:
Path(output_name).write_text(data)
with open(output_name, "w") as f:
f.write(data)
click.secho(f"LangChain migration script saved to {output_name}")
else:
click.secho(f"No migrations found for {pkg}", fg="yellow")
@@ -103,13 +104,13 @@ def partner(pkg: str, output: str) -> None:
@click.argument("json_file")
def json_to_grit(json_file: str) -> None:
"""Generate a Grit migration from an old JSON migration file."""
file = Path(json_file)
with file.open() as f:
with open(json_file) as f:
migrations = json.load(f)
name = file.stem
name = os.path.basename(json_file).removesuffix(".json").removesuffix(".grit")
data = dump_migrations_as_grit(name, migrations)
output_name = f"{name}.grit"
Path(output_name).write_text(data)
with open(output_name, "w") as f:
f.write(data)
click.secho(f"GritQL migration script saved to {output_name}")

View File

@@ -14,6 +14,3 @@ class File:
return False
return self.content == __value.content
def __hash__(self) -> int:
return hash((self.name, self.content))

View File

@@ -57,6 +57,3 @@ class Folder:
return False
return True
def __hash__(self) -> int:
return hash((self.name, tuple(self.files)))

View File

@@ -21,13 +21,13 @@ For full documentation see the [API reference](https://python.langchain.com/api_
## 1⃣ Core Interface: Runnables
The concept of a `Runnable` is central to LangChain Core it is the interface that most LangChain Core components implement, giving them
The concept of a Runnable is central to LangChain Core it is the interface that most LangChain Core components implement, giving them
- a common invocation interface (`invoke()`, `batch()`, `stream()`, etc.)
- a common invocation interface (invoke, batch, stream, etc.)
- built-in utilities for retries, fallbacks, schemas and runtime configurability
- easy deployment with [LangGraph](https://github.com/langchain-ai/langgraph)
- easy deployment with [LangServe](https://github.com/langchain-ai/langserve)
For more check out the [runnable docs](https://python.langchain.com/docs/concepts/runnables/). Examples of components that implement the interface include: LLMs, Chat Models, Prompts, Retrievers, Tools, Output Parsers.
For more check out the [runnable docs](https://python.langchain.com/docs/expression_language/interface). Examples of components that implement the interface include: LLMs, Chat Models, Prompts, Retrievers, Tools, Output Parsers.
You can use LangChain Core objects in two ways:
@@ -51,7 +51,7 @@ LangChain Expression Language (LCEL) is a _declarative language_ for composing L
LangChain Core compiles LCEL sequences to an _optimized execution plan_, with automatic parallelization, streaming, tracing, and async support.
For more check out the [LCEL docs](https://python.langchain.com/docs/concepts/lcel/).
For more check out the [LCEL docs](https://python.langchain.com/docs/expression_language/).
![Diagram outlining the hierarchical organization of the LangChain framework, displaying the interconnected parts across multiple layers.](https://raw.githubusercontent.com/langchain-ai/langchain/master/docs/static/svg/langchain_stack_112024.svg "LangChain Framework Overview")
@@ -59,6 +59,8 @@ For more advanced use cases, also check out [LangGraph](https://github.com/langc
## 📕 Releases & Versioning
`langchain-core` is currently on version `0.1.x`.
As `langchain-core` contains the base abstractions and runtime for the whole LangChain ecosystem, we will communicate any breaking changes with advance notice and version bumps. The exception for this is anything in `langchain_core.beta`. The reason for `langchain_core.beta` is that given the rate of change of the field, being able to move quickly is still a priority, and this module is our attempt to do so.
Minor version increases will occur for:

View File

@@ -253,7 +253,7 @@ class ContextSet(RunnableSerializable):
"""
if key is not None:
kwargs[key] = value
super().__init__(
super().__init__( # type: ignore[call-arg]
keys={
k: _coerce_set_value(v) if v is not None else None
for k, v in kwargs.items()

View File

@@ -277,7 +277,7 @@ class Document(BaseMedia):
"""Pass page_content in as positional or named arg."""
# my-py is complaining that page_content is not defined on the base class.
# Here, we're relying on pydantic base class to handle the validation.
super().__init__(page_content=page_content, **kwargs)
super().__init__(page_content=page_content, **kwargs) # type: ignore[call-arg]
@classmethod
def is_lc_serializable(cls) -> bool:

View File

@@ -533,7 +533,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
generations = [generations_with_error_metadata]
run_manager.on_llm_error(
e,
response=LLMResult(generations=generations),
response=LLMResult(generations=generations), # type: ignore[arg-type]
)
raise
@@ -627,7 +627,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
generations = [generations_with_error_metadata]
await run_manager.on_llm_error(
e,
response=LLMResult(generations=generations),
response=LLMResult(generations=generations), # type: ignore[arg-type]
)
raise
@@ -842,17 +842,17 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
run_managers[i].on_llm_error(
e,
response=LLMResult(
generations=[generations_with_error_metadata]
generations=[generations_with_error_metadata] # type: ignore[list-item]
),
)
raise
flattened_outputs = [
LLMResult(generations=[res.generations], llm_output=res.llm_output)
LLMResult(generations=[res.generations], llm_output=res.llm_output) # type: ignore[list-item]
for res in results
]
llm_output = self._combine_llm_outputs([res.llm_output for res in results])
generations = [res.generations for res in results]
output = LLMResult(generations=generations, llm_output=llm_output)
output = LLMResult(generations=generations, llm_output=llm_output) # type: ignore[arg-type]
if run_managers:
run_infos = []
for manager, flattened_output in zip(run_managers, flattened_outputs):
@@ -962,7 +962,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
await run_managers[i].on_llm_error(
res,
response=LLMResult(
generations=[generations_with_error_metadata]
generations=[generations_with_error_metadata] # type: ignore[list-item]
),
)
exceptions.append(res)
@@ -972,7 +972,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
*[
run_manager.on_llm_end(
LLMResult(
generations=[res.generations], # type: ignore[union-attr]
generations=[res.generations], # type: ignore[list-item, union-attr]
llm_output=res.llm_output, # type: ignore[union-attr]
)
)
@@ -982,12 +982,12 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
)
raise exceptions[0]
flattened_outputs = [
LLMResult(generations=[res.generations], llm_output=res.llm_output) # type: ignore[union-attr]
LLMResult(generations=[res.generations], llm_output=res.llm_output) # type: ignore[list-item, union-attr]
for res in results
]
llm_output = self._combine_llm_outputs([res.llm_output for res in results]) # type: ignore[union-attr]
generations = [res.generations for res in results] # type: ignore[union-attr]
output = LLMResult(generations=generations, llm_output=llm_output)
output = LLMResult(generations=generations, llm_output=llm_output) # type: ignore[arg-type]
await asyncio.gather(
*[
run_manager.on_llm_end(flattened_output)

View File

@@ -358,10 +358,7 @@ class AIMessageChunk(AIMessage, BaseMessageChunk):
for chunk in self.tool_call_chunks:
try:
if chunk["args"] is not None and chunk["args"] != "":
args_ = parse_partial_json(chunk["args"])
else:
args_ = {}
args_ = parse_partial_json(chunk["args"]) if chunk["args"] != "" else {} # type: ignore[arg-type]
if isinstance(args_, dict):
tool_calls.append(
create_tool_call(

View File

@@ -155,7 +155,9 @@ class MessagesPlaceholder(BaseMessagePromptTemplate):
"""
# mypy can't detect the init which is defined in the parent class
# b/c these are BaseModel classes.
super().__init__(variable_name=variable_name, optional=optional, **kwargs)
super().__init__( # type: ignore[call-arg]
variable_name=variable_name, optional=optional, **kwargs
)
def format_messages(self, **kwargs: Any) -> list[BaseMessage]:
"""Format messages from kwargs.

View File

@@ -2819,7 +2819,7 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
if len(steps_flat) < 2:
msg = f"RunnableSequence must have at least 2 steps, got {len(steps_flat)}"
raise ValueError(msg)
super().__init__(
super().__init__( # type: ignore[call-arg]
first=steps_flat[0],
middle=list(steps_flat[1:-1]),
last=steps_flat[-1],
@@ -3612,7 +3612,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
"""
merged = {**steps__} if steps__ is not None else {}
merged.update(kwargs)
super().__init__(
super().__init__( # type: ignore[call-arg]
steps__={key: coerce_to_runnable(r) for key, r in merged.items()}
)
@@ -5325,7 +5325,7 @@ class RunnableEach(RunnableEachBase[Input, Output]):
)
class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[no-redef]
class RunnableBindingBase(RunnableSerializable[Input, Output]):
"""``Runnable`` that delegates calls to another ``Runnable`` with a set of kwargs.
Use only if creating a new ``RunnableBinding`` subclass with different ``__init__``
@@ -5404,7 +5404,7 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
``Runnable`` with a custom type. Defaults to None.
**other_kwargs: Unpacked into the base class.
""" # noqa: E501
super().__init__(
super().__init__( # type: ignore[call-arg]
bound=bound,
kwargs=kwargs or {},
config=config or {},
@@ -5729,7 +5729,7 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[
yield item
class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
class RunnableBinding(RunnableBindingBase[Input, Output]):
"""Wrap a ``Runnable`` with additional functionality.
A ``RunnableBinding`` can be thought of as a "runnable decorator" that

View File

@@ -136,7 +136,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
super().__init__(
branches=branches_,
default=default_,
)
) # type: ignore[call-arg]
model_config = ConfigDict(
arbitrary_types_allowed=True,

View File

@@ -38,7 +38,7 @@ MessagesOrDictWithMessages = Union[Sequence["BaseMessage"], dict[str, Any]]
GetSessionHistoryCallable = Callable[..., BaseChatMessageHistory]
class RunnableWithMessageHistory(RunnableBindingBase): # type: ignore[no-redef]
class RunnableWithMessageHistory(RunnableBindingBase):
"""Runnable that manages chat message history for another Runnable.
A chat message history is a sequence of messages that represent a conversation.

View File

@@ -186,7 +186,7 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]):
afunc = func
func = None
super().__init__(func=func, afunc=afunc, input_type=input_type, **kwargs)
super().__init__(func=func, afunc=afunc, input_type=input_type, **kwargs) # type: ignore[call-arg]
@classmethod
@override
@@ -406,7 +406,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):
mapper: A ``RunnableParallel`` instance that will be used to transform the
input dictionary.
"""
super().__init__(mapper=mapper, **kwargs)
super().__init__(mapper=mapper, **kwargs) # type: ignore[call-arg]
@classmethod
@override
@@ -710,7 +710,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):
Args:
keys: A single key or a list of keys to pick from the input dictionary.
"""
super().__init__(keys=keys, **kwargs)
super().__init__(keys=keys, **kwargs) # type: ignore[call-arg]
@classmethod
@override

View File

@@ -47,7 +47,7 @@ class ExponentialJitterParams(TypedDict, total=False):
"""Random additional wait sampled from random.uniform(0, jitter)."""
class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
class RunnableRetry(RunnableBindingBase[Input, Output]):
"""Retry a Runnable if it fails.
RunnableRetry can be used to add retry logic to any object

View File

@@ -87,7 +87,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
Args:
runnables: A mapping of keys to Runnables.
"""
super().__init__(
super().__init__( # type: ignore[call-arg]
runnables={key: coerce_to_runnable(r) for key, r in runnables.items()}
)

View File

@@ -143,7 +143,7 @@ class Comparison(FilterDirective):
value: The value to compare to.
"""
# super exists from BaseModel
super().__init__(
super().__init__( # type: ignore[call-arg]
comparator=comparator, attribute=attribute, value=value, **kwargs
)
@@ -166,7 +166,9 @@ class Operation(FilterDirective):
arguments: The arguments to the operator.
"""
# super exists from BaseModel
super().__init__(operator=operator, arguments=arguments, **kwargs)
super().__init__( # type: ignore[call-arg]
operator=operator, arguments=arguments, **kwargs
)
class StructuredQuery(Expr):
@@ -194,4 +196,6 @@ class StructuredQuery(Expr):
limit: The limit on the number of results.
"""
# super exists from BaseModel
super().__init__(query=query, filter=filter, limit=limit, **kwargs)
super().__init__( # type: ignore[call-arg]
query=query, filter=filter, limit=limit, **kwargs
)

View File

@@ -392,6 +392,10 @@ class ToolException(Exception): # noqa: N818
ArgsSchema = Union[TypeBaseModel, dict[str, Any]]
"""Used to define the schema for tool arguments.
This schema should not include injected arguments like `InjectedToolCallId`.
"""
class BaseTool(RunnableSerializable[Union[str, dict, ToolCall], Any]):
@@ -581,11 +585,17 @@ class ChildTool(BaseTool):
) -> type[BaseModel]:
"""The tool's input schema.
All required arguments will be included as part of this schema, including
any injected arguments like `InjectedToolCallId`.
A schema without injected arguments can be obtained using the
`tool_call_schema` property instead.
Args:
config: The configuration for the tool.
Returns:
The input schema for the tool.
The full input schema for the tool, including injected arguments.
"""
if self.args_schema is not None:
if isinstance(self.args_schema, dict):
@@ -632,29 +642,29 @@ class ChildTool(BaseTool):
InjectedToolCallId is required but not provided.
NotImplementedError: If args_schema is not a supported type.
"""
input_args = self.args_schema
args_schema = self.args_schema
if isinstance(tool_input, str):
if input_args is not None:
if isinstance(input_args, dict):
if args_schema is not None:
if isinstance(args_schema, dict):
msg = (
"String tool inputs are not allowed when "
"using tools with JSON schema args_schema."
)
raise ValueError(msg)
key_ = next(iter(get_fields(input_args).keys()))
if issubclass(input_args, BaseModel):
input_args.model_validate({key_: tool_input})
elif issubclass(input_args, BaseModelV1):
input_args.parse_obj({key_: tool_input})
key_ = next(iter(get_fields(args_schema).keys()))
if issubclass(args_schema, BaseModel):
args_schema.model_validate({key_: tool_input})
elif issubclass(args_schema, BaseModelV1):
args_schema.parse_obj({key_: tool_input})
else:
msg = f"args_schema must be a Pydantic BaseModel, got {input_args}"
msg = f"args_schema must be a Pydantic BaseModel, got {args_schema}"
raise TypeError(msg)
return tool_input
if input_args is not None:
if isinstance(input_args, dict):
if args_schema is not None:
if isinstance(args_schema, dict):
return tool_input
if issubclass(input_args, BaseModel):
for k, v in get_all_basemodel_annotations(input_args).items():
if issubclass(args_schema, BaseModel):
for k, v in get_all_basemodel_annotations(args_schema).items():
if (
_is_injected_arg_type(v, injected_type=InjectedToolCallId)
and k not in tool_input
@@ -669,10 +679,10 @@ class ChildTool(BaseTool):
)
raise ValueError(msg)
tool_input[k] = tool_call_id
result = input_args.model_validate(tool_input)
result = args_schema.model_validate(tool_input)
result_dict = result.model_dump()
elif issubclass(input_args, BaseModelV1):
for k, v in get_all_basemodel_annotations(input_args).items():
elif issubclass(args_schema, BaseModelV1):
for k, v in get_all_basemodel_annotations(args_schema).items():
if (
_is_injected_arg_type(v, injected_type=InjectedToolCallId)
and k not in tool_input
@@ -687,7 +697,7 @@ class ChildTool(BaseTool):
)
raise ValueError(msg)
tool_input[k] = tool_call_id
result = input_args.parse_obj(tool_input)
result = args_schema.parse_obj(tool_input)
result_dict = result.dict()
else:
msg = (

View File

@@ -228,7 +228,7 @@ class StructuredTool(BaseTool):
name=name,
func=func,
coroutine=coroutine,
args_schema=args_schema,
args_schema=args_schema, # type: ignore[arg-type]
description=description_,
return_direct=return_direct,
response_format=response_format,

View File

@@ -1,16 +1,15 @@
"""Internal tracers used for stream_log and astream events implementations."""
import typing
import abc
from collections.abc import AsyncIterator, Iterator
from typing import TypeVar
from uuid import UUID
T = typing.TypeVar("T")
T = TypeVar("T")
# THIS IS USED IN LANGGRAPH.
@typing.runtime_checkable
class _StreamingCallbackHandler(typing.Protocol[T]):
"""Types for streaming callback handlers.
class _StreamingCallbackHandler(abc.ABC):
"""For internal use.
This is a common mixin that the callback handlers
for both astream events and astream log inherit from.
@@ -19,11 +18,13 @@ class _StreamingCallbackHandler(typing.Protocol[T]):
to produce callbacks for intermediate results.
"""
@abc.abstractmethod
def tap_output_aiter(
self, run_id: UUID, output: AsyncIterator[T]
) -> AsyncIterator[T]:
"""Used for internal astream_log and astream events implementations."""
@abc.abstractmethod
def tap_output_iter(self, run_id: UUID, output: Iterator[T]) -> Iterator[T]:
"""Used for internal astream_log and astream events implementations."""

View File

@@ -64,7 +64,6 @@ langchain-text-splitters = { path = "../text-splitters" }
[tool.mypy]
plugins = ["pydantic.mypy"]
strict = "True"
strict_bytes = "True"
enable_error_code = "deprecated"

View File

@@ -810,7 +810,7 @@ def test_parse_with_different_pydantic_2_v1() -> None:
# Can't get pydantic to work here due to the odd typing of tryig to support
# both v1 and v2 in the same codebase.
parser = PydanticToolsParser(tools=[Forecast])
parser = PydanticToolsParser(tools=[Forecast]) # type: ignore[list-item]
message = AIMessage(
content="",
tool_calls=[

View File

@@ -13,7 +13,7 @@ from langchain_core.language_models import ParrotFakeChatModel
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.output_parsers.json import JsonOutputParser
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.utils.pydantic import PydanticBaseModel, TBaseModel
from langchain_core.utils.pydantic import TBaseModel
class ForecastV2(pydantic.BaseModel):
@@ -43,7 +43,7 @@ def test_pydantic_parser_chaining(
model = ParrotFakeChatModel()
parser = PydanticOutputParser[PydanticBaseModel](pydantic_object=pydantic_object)
parser = PydanticOutputParser(pydantic_object=pydantic_object) # type: ignore[type-var]
chain = prompt | model | parser
res = chain.invoke({})
@@ -66,9 +66,7 @@ def test_pydantic_parser_validation(pydantic_object: TBaseModel) -> None:
model = ParrotFakeChatModel()
parser: PydanticOutputParser[PydanticBaseModel] = PydanticOutputParser(
pydantic_object=pydantic_object
)
parser = PydanticOutputParser(pydantic_object=pydantic_object) # type: ignore[arg-type,var-annotated]
chain = bad_prompt | model | parser
with pytest.raises(OutputParserException):
chain.invoke({})
@@ -90,7 +88,7 @@ def test_json_parser_chaining(
model = ParrotFakeChatModel()
parser = JsonOutputParser(pydantic_object=pydantic_object)
parser = JsonOutputParser(pydantic_object=pydantic_object) # type: ignore[arg-type]
chain = prompt | model | parser
res = chain.invoke({})
@@ -173,7 +171,7 @@ def test_pydantic_output_parser_type_inference() -> None:
# Ignoring mypy error that appears in python 3.8, but not 3.11.
# This seems to be functionally correct, so we'll ignore the error.
pydantic_parser = PydanticOutputParser[SampleModel](pydantic_object=SampleModel)
pydantic_parser = PydanticOutputParser(pydantic_object=SampleModel)
schema = pydantic_parser.get_output_schema().model_json_schema()
assert schema == {
@@ -204,5 +202,5 @@ def test_format_instructions_preserves_language() -> None:
)
)
parser = PydanticOutputParser[Foo](pydantic_object=Foo)
parser = PydanticOutputParser(pydantic_object=Foo)
assert description in parser.get_format_instructions()

View File

@@ -348,7 +348,7 @@ def test_prompt_invalid_template_format() -> None:
PromptTemplate(
input_variables=input_variables,
template=template,
template_format="bar",
template_format="bar", # type: ignore[arg-type]
)

View File

@@ -73,7 +73,7 @@ class MyOtherRunnable(RunnableSerializable[str, str]):
def test_doubly_set_configurable() -> None:
"""Test that setting a configurable field with a default value works."""
runnable = MyRunnable(my_property="a")
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",
@@ -86,7 +86,7 @@ def test_doubly_set_configurable() -> None:
def test_alias_set_configurable() -> None:
runnable = MyRunnable(my_property="a")
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property_alias",
@@ -104,7 +104,7 @@ def test_alias_set_configurable() -> None:
def test_field_alias_set_configurable() -> None:
runnable = MyRunnable(my_property_alias="a") # type: ignore[call-arg]
runnable = MyRunnable(my_property_alias="a")
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",
@@ -122,7 +122,7 @@ def test_field_alias_set_configurable() -> None:
def test_config_passthrough() -> None:
runnable = MyRunnable(my_property="a")
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",
@@ -158,7 +158,7 @@ def test_config_passthrough() -> None:
def test_config_passthrough_nested() -> None:
runnable = MyRunnable(my_property="a")
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",

View File

@@ -455,9 +455,9 @@ def test_message_chunk_to_message() -> None:
tool_calls=[
create_tool_call(name="tool1", args={"a": 1}, id="1"),
create_tool_call(name="tool2", args={}, id="2"),
create_tool_call(name="tool3", args={}, id="3"),
],
invalid_tool_calls=[
create_invalid_tool_call(name="tool3", args=None, id="3", error=None),
create_invalid_tool_call(name="tool4", args="abc", id="4", error=None),
],
)

View File

@@ -2705,3 +2705,79 @@ def test_tool_args_schema_with_annotated_type() -> None:
"type": "array",
}
}
def test_args_schema_without_injected_arguments() -> None:
"""Test that injected args work correctly in tool schemas."""
class CustomSchema(BaseModel):
"""Schema for LLM inputs."""
text: str = Field(description="Text to process")
# InjectedToolCallId should NOT need to be here
@tool(
"broken_tool",
description="Tool that should work with automatic injection",
parse_docstring=True,
args_schema=CustomSchema,
)
def broken_tool(
text: str,
tool_call_id: Annotated[str, InjectedToolCallId],
) -> str:
"""Tool with injected arguments."""
return f"Processed '{text}' (call_id: {tool_call_id})"
# Test tool call structure
tool_call = {
"name": "test_tool",
"args": {"text": "test data"},
"id": "test_123",
"type": "tool_call",
}
# The full input schema should include tool_call_id for validation
schema = _schema(broken_tool.get_input_schema())
assert schema == {
"description": "Tool with injected arguments.",
"properties": {
"text": {
"title": "Text",
"type": "string",
"description": "Text to process",
},
# Tool call ID **is** required in this case.
"tool_call_id": {
"title": "Tool Call Id",
"type": "string",
},
},
"required": [
"text",
"tool_call_id",
],
"title": "CustomSchema",
"type": "object",
}
result = broken_tool.invoke(tool_call)
assert isinstance(result, ToolMessage)
assert result.content == "Processed 'test data' (call_id: test_123)"
# The tool schema does not include injectable arguments that are hidden from the
# LLM.
tool_call_schema = _schema(broken_tool.tool_call_schema)
assert tool_call_schema == {
"description": "Tool that should work with automatic injection",
"properties": {
"text": {
"description": "Text to process",
"title": "Text",
"type": "string",
}
},
"required": ["text"],
"title": "broken_tool",
"type": "object",
}

View File

@@ -347,7 +347,7 @@ def test_using_secret_from_env_as_default_factory(
secret: SecretStr = Field(default_factory=secret_from_env("TEST_KEY"))
# Pass the secret as a parameter
foo = Foo(secret="super_secret")
foo = Foo(secret="super_secret") # type: ignore[arg-type]
assert foo.secret.get_secret_value() == "super_secret"
# Set the environment variable

2
libs/core/uv.lock generated
View File

@@ -1222,7 +1222,7 @@ test-integration = [
]
typing = [
{ name = "lxml-stubs", specifier = ">=0.5.1,<1.0.0" },
{ name = "mypy", specifier = ">=1.17.1,<1.18" },
{ name = "mypy", specifier = ">=1.15,<2.0" },
{ name = "tiktoken", specifier = ">=0.8.0,<1.0.0" },
{ name = "types-requests", specifier = ">=2.31.0.20240218,<3.0.0.0" },
]

View File

@@ -3,21 +3,28 @@
⚡ Building applications with LLMs through composability ⚡
[![Release Notes](https://img.shields.io/github/release/langchain-ai/langchain)](https://github.com/langchain-ai/langchain/releases)
[![lint](https://github.com/langchain-ai/langchain/actions/workflows/lint.yml/badge.svg)](https://github.com/langchain-ai/langchain/actions/workflows/lint.yml)
[![test](https://github.com/langchain-ai/langchain/actions/workflows/test.yml/badge.svg)](https://github.com/langchain-ai/langchain/actions/workflows/test.yml)
[![Downloads](https://static.pepy.tech/badge/langchain/month)](https://pepy.tech/project/langchain)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/langchainai.svg?style=social&label=Follow%20%40LangChainAI)](https://twitter.com/langchainai)
[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/langchain-ai/langchain)
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/langchain-ai/langchain)
[![GitHub star chart](https://img.shields.io/github/stars/langchain-ai/langchain?style=social)](https://star-history.com/#langchain-ai/langchain)
[![Dependency Status](https://img.shields.io/librariesio/github/langchain-ai/langchain)](https://libraries.io/github/langchain-ai/langchain)
[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langchain)](https://github.com/langchain-ai/langchain/issues)
Looking for the JS/TS version? Check out [LangChain.js](https://github.com/langchain-ai/langchainjs).
To help you ship LangChain apps to production faster, check out [LangSmith](https://smith.langchain.com).
[LangSmith](https://smith.langchain.com) is a unified developer platform for building, testing, and monitoring LLM applications.
Fill out [this form](https://www.langchain.com/contact-sales) to speak with our sales team.
## Quick Install
`pip install langchain`
or
`pip install langsmith && conda install langchain -c conda-forge`
## 🤔 What is this?
@@ -27,22 +34,22 @@ This library aims to assist in the development of those types of applications. C
**❓ Question answering with RAG**
- [Documentation](https://python.langchain.com/docs/tutorials/rag/)
- [Documentation](https://python.langchain.com/docs/use_cases/question_answering/)
- End-to-end Example: [Chat LangChain](https://chat.langchain.com) and [repo](https://github.com/langchain-ai/chat-langchain)
**🧱 Extracting structured output**
- [Documentation](https://python.langchain.com/docs/tutorials/extraction/)
- [Documentation](https://python.langchain.com/docs/use_cases/extraction/)
- End-to-end Example: [SQL Llama2 Template](https://github.com/langchain-ai/langchain-extract/)
**🤖 Chatbots**
- [Documentation](https://python.langchain.com/docs/tutorials/chatbot/)
- [Documentation](https://python.langchain.com/docs/use_cases/chatbots)
- End-to-end Example: [Web LangChain (web researcher chatbot)](https://weblangchain.vercel.app) and [repo](https://github.com/langchain-ai/weblangchain)
## 📖 Documentation
Please see [our full documentation](https://python.langchain.com) on:
Please see [here](https://python.langchain.com) for full documentation on:
- Getting started (installation, setting up the environment, simple examples)
- How-To examples (demos, integrations, helper functions)
@@ -72,7 +79,7 @@ Agents involve an LLM making decisions about which Actions to take, taking that
**🧐 Evaluation:**
Generative models are notoriously hard to evaluate with traditional metrics. One new way of evaluating them is using language models themselves to do the evaluation. LangChain provides some prompts/chains for assisting in this.
[BETA] Generative models are notoriously hard to evaluate with traditional metrics. One new way of evaluating them is using language models themselves to do the evaluation. LangChain provides some prompts/chains for assisting in this.
For more information on these concepts, please see our [full documentation](https://python.langchain.com).

View File

@@ -1375,7 +1375,7 @@ class AgentExecutor(Chain):
elif callable(self.handle_parsing_errors):
observation = self.handle_parsing_errors(e)
else:
msg = "Got unexpected type of `handle_parsing_errors`" # type: ignore[unreachable]
msg = "Got unexpected type of `handle_parsing_errors`"
raise ValueError(msg) from e # noqa: TRY004
output = AgentAction("_Exception", observation, text)
if run_manager:
@@ -1514,7 +1514,7 @@ class AgentExecutor(Chain):
elif callable(self.handle_parsing_errors):
observation = self.handle_parsing_errors(e)
else:
msg = "Got unexpected type of `handle_parsing_errors`" # type: ignore[unreachable]
msg = "Got unexpected type of `handle_parsing_errors`"
raise ValueError(msg) from e # noqa: TRY004
output = AgentAction("_Exception", observation, text)
tool_run_kwargs = self._action_agent.tool_run_logging_kwargs()
@@ -1551,7 +1551,7 @@ class AgentExecutor(Chain):
],
)
# TODO: This could yield each result as it becomes available
# TODO This could yield each result as it becomes available
for chunk in result:
yield chunk

View File

@@ -1,6 +1,5 @@
import json
from collections.abc import Sequence
from typing import Any
from langchain_core.agents import AgentAction, AgentActionMessageLog
from langchain_core.messages import AIMessage, BaseMessage, FunctionMessage
@@ -31,7 +30,7 @@ def _convert_agent_action_to_messages(
def _create_function_message(
agent_action: AgentAction,
observation: Any,
observation: str,
) -> FunctionMessage:
"""Convert agent action and observation into a function message.
Args:

View File

@@ -1,6 +1,5 @@
import json
from collections.abc import Sequence
from typing import Any
from langchain_core.agents import AgentAction
from langchain_core.messages import (
@@ -14,7 +13,7 @@ from langchain.agents.output_parsers.tools import ToolAgentAction
def _create_tool_message(
agent_action: ToolAgentAction,
observation: Any,
observation: str,
) -> ToolMessage:
"""Convert agent action and observation into a tool message.

View File

@@ -14,7 +14,7 @@ from langchain.agents.agent import BaseMultiActionAgent, BaseSingleActionAgent
from langchain.agents.types import AGENT_TO_CLASS
from langchain.chains.loading import load_chain, load_chain_from_config
logger = logging.getLogger(__name__)
logger = logging.getLogger(__file__)
URL_BASE = "https://raw.githubusercontent.com/hwchase17/langchain-hub/master/agents/"

Some files were not shown because too many files have changed in this diff Show More