Skip to content

Pydantic type conversion to SecreteStr for ChatAnthropic anthropic_api_key keyword value raises attribute error in Anthropic client #11182

@RubenTurciosTensoriot

Description

@RubenTurciosTensoriot

System Info

Amazon Linux, langchain version 0.0.304, Docker Image

Who can help?

@hwchase17
@agola11

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Steps to reproduce:

  1. Initialize ChatAnthropic model, with keyword "anthropic_api_key", as documented: https://api.python.langchain.com/en/latest/chat_models/langchain.chat_models.anthropic.ChatAnthropic.html#langchain.chat_models.anthropic.ChatAnthropic

  2. Call chat model via call method, e.g.:
    """
    chat = ChatAnthropic(..., anthropic_api_key=os.environ["ANTHROPIC_API_KEY"])
    messages = [....]
    response = chat(messages)
    """

Expected behavior

A normal response for a chat model is expected, but instead we get:

Traceback (most recent call last):
  File "/var/task/app/src/utils/error_route.py", line 27, in custom_route_handler
    return await original_route_handler(request)
  File "/var/lang/lib/python3.10/site-packages/fastapi/routing.py", line 273, in app
    raw_response = await run_endpoint_function(
  File "/var/lang/lib/python3.10/site-packages/fastapi/routing.py", line 190, in run_endpoint_function
    return await dependant.call(**values)
  File "/var/task/app/app.py", line 184, in chat
    reply = chat(data["question"])
  File "/var/task/app/src/app_modules/chat.py", line 192, in __call__
    res = self.chatbot(messages)
  File "/var/lang/lib/python3.10/site-packages/langchain/chat_models/base.py", line 595, in __call__
    generation = self.generate(
  File "/var/lang/lib/python3.10/site-packages/langchain/chat_models/base.py", line 348, in generate
    raise e
  File "/var/lang/lib/python3.10/site-packages/langchain/chat_models/base.py", line 338, in generate
    self._generate_with_cache(
  File "/var/lang/lib/python3.10/site-packages/langchain/chat_models/base.py", line 490, in _generate_with_cache
    return self._generate(
  File "/var/lang/lib/python3.10/site-packages/langchain/chat_models/anthropic.py", line 184, in _generate
    response = self.client.completions.create(**params)
  File "/var/lang/lib/python3.10/site-packages/anthropic/_utils/_utils.py", line 250, in wrapper
    return func(*args, **kwargs)
  File "/var/lang/lib/python3.10/site-packages/anthropic/resources/completions.py", line 223, in create
    return self._post(
  File "/var/lang/lib/python3.10/site-packages/anthropic/_base_client.py", line 949, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "/var/lang/lib/python3.10/site-packages/anthropic/_base_client.py", line 748, in request
    return self._request(
  File "/var/lang/lib/python3.10/site-packages/anthropic/_base_client.py", line 766, in _request
    request = self._build_request(options)
  File "/var/lang/lib/python3.10/site-packages/anthropic/_base_client.py", line 397, in _build_request
    headers = self._build_headers(options)
  File "/var/lang/lib/python3.10/site-packages/anthropic/_base_client.py", line 373, in _build_headers
    headers = httpx.Headers(headers_dict)
  File "/var/lang/lib/python3.10/site-packages/httpx/_models.py", line 71, in __init__
    self._list = [
  File "/var/lang/lib/python3.10/site-packages/httpx/_models.py", line 75, in <listcomp>
    normalize_header_value(v, encoding),
  File "/var/lang/lib/python3.10/site-packages/httpx/_utils.py", line 53, in normalize_header_value
    return value.encode(encoding or "ascii")
AttributeError: 'SecretStr' object has no attribute 'encode'

Corresponding httpx code:

def normalize_header_value(
    value: typing.Union[str, bytes], encoding: typing.Optional[str] = None
) -> bytes:
    """
    Coerce str/bytes into a strictly byte-wise HTTP header value.
    """
    if isinstance(value, bytes):
        return value
    return value.encode(encoding or "ascii")

Metadata

Metadata

Assignees

Labels

bugRelated to a bug, vulnerability, unexpected error with an existing feature

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions