langchain-ai / langchain-aws

Build LangChain Applications on AWS
MIT License
103 stars 81 forks source link

ChatBedrockConverse doesn't take into account boto3 setup_default_session. #157

Open thiagotps opened 2 months ago

thiagotps commented 2 months ago

The following code:

import boto3
from langchain_aws import ChatBedrockConverse

boto3.setup_default_session(profile_name="genai-dev", region_name="us-east-1")
llm = ChatBedrockConverse(model="anthropic.claude-3-haiku-20240307-v1:0")

raises the following error:

ValidationError: 1 validation error for ChatBedrockConverse
__root__
  Did not find region_name, please add an environment variable `AWS_DEFAULT_REGION` which contains it, or pass `region_name` as a named parameter. (type=value_error)

The error is asking for the environmental variable AWS_DEFAULT_REGION, even though I have set the region name via boto3 setup_default_session method.

isahers1 commented 2 months ago

I believe you need to pass region_name directly into ChatBedrockConverse - or set the environment variable AWS_DEFAULT_REGION. boto3 and langchain_aws don't share variables, so you must specify the region name for the llm as well I believe. Let me know if that fixes your error.

thiagotps commented 2 months ago

I believe you need to pass region_name directly into ChatBedrockConverse - or set the environment variable AWS_DEFAULT_REGION. boto3 and langchain_aws don't share variables, so you must specify the region name for the llm as well I believe. Let me know if that fixes your error.

Yes, setting region_name and credentials_profile_name in the ChatBedrockConverse constructor works.

Looking in the code of validate_environment in bedrock_converse.py, I see that a new boto3.Session object is being instantiated, and later, with that new session, the bedrock-runtime client is then created. Wouldn't it be better to just call boto3.client('bedrock-runtime') directly (or at least when no credentials_profile_name was provided) ? That way it will work with boto3.setup_default_session out of the box.

isahers1 commented 2 months ago

Not a boto3 expert by any means - but if I understand correctly the code currently creates its own session, but you would like it to work with the session already setup? That sounds reasonable to me - if you want to open up a PR (with a corresponding test case) I would be happy to review. Lmk if I misunderstood

3coins commented 3 weeks ago

@thiagotps I believe this has been fixed in the latest release. I am not able to reproduce with v0.2.2. Let us know if you still see this issue.