Skip to content

Commit

Permalink
Passthrough configurable primitive values as tracer metadata (langcha…
Browse files Browse the repository at this point in the history
…in-ai#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.
 -->
  • Loading branch information
nfcampos committed Jan 12, 2024
1 parent 129552e commit 112208b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libs/core/langchain_core/runnables/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
36 changes: 36 additions & 0 deletions libs/core/tests/unit_tests/runnables/test_runnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
add,
chain,
)
from langchain_core.runnables.base import RunnableSerializable
from langchain_core.tools import BaseTool, tool
from langchain_core.tracers import (
BaseTracer,
Expand Down Expand Up @@ -142,6 +143,17 @@ def invoke(
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,
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 112208b

Please sign in to comment.