Passthrough configurable primitive values as tracer metadata (#15915)

<!-- Thank you for contributing to LangChain!

Please title your PR "<package>: <description>", where <package> is
whichever of langchain, community, core, experimental, etc. is being
modified.

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes if applicable,
  - **Dependencies:** any dependencies required for this change,
- **Twitter handle:** we announce bigger features on Twitter. If your PR
gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` from the root
of the package you've modified to check this locally.

See contribution guidelines for more information on how to write/run
tests, lint, etc: https://python.langchain.com/docs/contributing/

If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.

If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17.
 -->
This commit is contained in:
Nuno Campos 2024-01-11 18:47:55 -08:00 committed by GitHub
parent 129552e3d6
commit 112208baa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -125,6 +125,9 @@ def ensure_config(config: Optional[RunnableConfig] = None) -> RunnableConfig:
empty.update(
cast(RunnableConfig, {k: v for k, v in config.items() if v is not None})
)
for key, value in empty.get("configurable", {}).items():
if isinstance(value, (str, int, float, bool)) and key not in empty["metadata"]:
empty["metadata"][key] = value
return empty

View File

@ -70,6 +70,7 @@ from langchain_core.runnables import (
add,
chain,
)
from langchain_core.runnables.base import RunnableSerializable
from langchain_core.tools import BaseTool, tool
from langchain_core.tracers import (
BaseTracer,
@ -142,6 +143,17 @@ class FakeRunnable(Runnable[str, int]):
return len(input)
class FakeRunnableSerializable(RunnableSerializable[str, int]):
hello: str = ""
def invoke(
self,
input: str,
config: Optional[RunnableConfig] = None,
) -> int:
return len(input)
class FakeRetriever(BaseRetriever):
def _get_relevant_documents(
self,
@ -1302,6 +1314,30 @@ async def test_passthrough_tap_async(mocker: MockerFixture) -> None:
mock.reset_mock()
async def test_with_config_metadata_passthrough(mocker: MockerFixture) -> None:
fake = FakeRunnableSerializable()
spy = mocker.spy(fake.__class__, "invoke")
fakew = fake.configurable_fields(hello=ConfigurableField(id="hello", name="Hello"))
assert (
fakew.with_config(tags=["a-tag"]).invoke(
"hello", {"configurable": {"hello": "there"}, "metadata": {"bye": "now"}}
)
== 5
)
assert spy.call_args_list[0].args[1:] == (
"hello",
dict(
tags=["a-tag"],
callbacks=None,
recursion_limit=25,
configurable={"hello": "there"},
metadata={"hello": "there", "bye": "now"},
),
)
spy.reset_mock()
async def test_with_config(mocker: MockerFixture) -> None:
fake = FakeRunnable()
spy = mocker.spy(fake, "invoke")