mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-01 19:12:42 +00:00
[Core] Feat: update config CVar in tool.invoke (#20808)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""Test the base tool implementation."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from functools import partial
|
||||
@@ -13,6 +15,7 @@ from langchain_core.callbacks import (
|
||||
CallbackManagerForToolRun,
|
||||
)
|
||||
from langchain_core.pydantic_v1 import BaseModel, ValidationError
|
||||
from langchain_core.runnables import ensure_config
|
||||
from langchain_core.tools import (
|
||||
BaseTool,
|
||||
SchemaAnnotationError,
|
||||
@@ -871,3 +874,34 @@ def test_tool_invoke_optional_args(inputs: dict, expected: Optional[dict]) -> No
|
||||
else:
|
||||
with pytest.raises(ValidationError):
|
||||
foo.invoke(inputs) # type: ignore
|
||||
|
||||
|
||||
def test_tool_pass_context() -> None:
|
||||
@tool
|
||||
def foo(bar: str) -> str:
|
||||
"""The foo."""
|
||||
config = ensure_config()
|
||||
assert config["configurable"]["foo"] == "not-bar"
|
||||
assert bar == "baz"
|
||||
return bar
|
||||
|
||||
assert foo.invoke({"bar": "baz"}, {"configurable": {"foo": "not-bar"}}) == "baz" # type: ignore
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.version_info < (3, 11),
|
||||
reason="requires python3.11 or higher",
|
||||
)
|
||||
async def test_async_tool_pass_context() -> None:
|
||||
@tool
|
||||
async def foo(bar: str) -> str:
|
||||
"""The foo."""
|
||||
await asyncio.sleep(0.0001)
|
||||
config = ensure_config()
|
||||
assert config["configurable"]["foo"] == "not-bar"
|
||||
assert bar == "baz"
|
||||
return bar
|
||||
|
||||
assert (
|
||||
await foo.ainvoke({"bar": "baz"}, {"configurable": {"foo": "not-bar"}}) == "baz" # type: ignore
|
||||
)
|
||||
|
Reference in New Issue
Block a user