langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.07k stars 14.66k forks source link

ChatAnthropic - Found metadata supplied twice #24121

Open mjkilian1992 opened 1 month ago

mjkilian1992 commented 1 month ago

Checked other resources

Example Code

import os

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables import ConfigurableField, RunnableConfig
from pydantic.v1 import SecretStr

client = ChatAnthropic(
    base_url=os.environ['CHAT_ANTHROPIC_BASE_URL'],
    api_key=SecretStr(os.environ['CHAT_ANTHROPIC_API_KEY']),
    model_name='claude-3-opus-20240229',
).configurable_fields(
    model_kwargs=ConfigurableField(
        id="model_kwargs",
        name="Model Kwargs",
        description="Keyword arguments to pass through to the chat client (e.g. user)",
    ),
)

configurable = {
    "model_kwargs": {"metadata": {"user_id": "testuserid"}}
}

response = client.invoke("Write me a short story", config=RunnableConfig(configurable=configurable))

print(response)

Error Message and Stack Trace (if applicable)

Exception: ValidatorError

Traceback (most recent call last):
  File "main.py", line 32, in <module>
    response = client.invoke("Write me a short story", config=RunnableConfig(configurable=configurable))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/langchain_core/runnables/configurable.py", line 115, in invoke
    runnable, config = self.prepare(config)
                       ^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/langchain_core/runnables/configurable.py", line 104, in prepare
    runnable, config = runnable._prepare(merge_configs(runnable.config, config))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/langchain_core/runnables/configurable.py", line 415, in _prepare
    self.default.__class__(**{**init_params, **configurable}),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for ChatAnthropic
__root__
  Found metadata supplied twice. (type=value_error)

Description

Is there a way around this so that I can pass the metadata kwarg to the create() call as expected? At a glance since it's nested under model_kwargs it shouldn't clash with other params. Are they being flattened and if so, why?

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 23.5.0: Wed May 1 20:14:38 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6020 Python Version: 3.12.3 (main, Jul 2 2024, 11:16:56) [Clang 15.0.0 (clang-1500.3.9.4)]

Package Information

langchain_core: 0.2.13 langchain: 0.2.7 langchain_community: 0.2.7 langsmith: 0.1.85 langchain_anthropic: 0.1.19 langchain_openai: 0.1.15 langchain_text_splitters: 0.2.2

Packages not installed (Not Necessarily a Problem)

The following packages were not found:

langgraph langserve

baskaryan commented 1 month ago

metadata should be passed in directly, not via model_kwargs

import os

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables import ConfigurableField, RunnableConfig

client = ChatAnthropic(
    model_name='claude-3-opus-20240229',
).configurable_fields(
    metadata=ConfigurableField(
        id="metadata",
        name="Metadata",
        description="metadata ",
    ),
)

configurable = {
    "metadata": {"user_id": "testuserid"}
}

response = client.invoke("Write me a short story", config=RunnableConfig(configurable=configurable))

print(response)
Screenshot 2024-07-11 at 10 35 49 AM
mjkilian1992 commented 1 month ago

I tried your suggestion @baskaryan and it no longer errors, but unfortunately it doesn't send the user ID either. Here's the request params that are sent on:

{
  "model": "claude-3-opus-20240229",
  "messages": [
    {
      "role": "user",
      "content": "Write me a short story"
    }
  ],
  "max_tokens": 1024
}