5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

langchain_awsのChatBedrockConverseで構造化出力を行うとエラーになる問題の対処法

Posted at

langchain_awsのChatBedrockConverseを使ってwith_structured_outputをするとエラーになる問題の対処法

ChatBedrockを使用するとOK

これはOK

from langchain_aws import ChatBedrock
from pydantic import BaseModel, Field


class SearchQuery(BaseModel):
    search_query: str = Field(None, description="ウェブ検索に最適化されたクエリ")


llm = ChatBedrock(model="anthropic.claude-3-haiku-20240307-v1:0")
llm = llm.with_structured_output(SearchQuery)

response = llm.invoke("Googleで'富士山'を検索")

print(response.search_query)

富士山

ChatBedrockConverseを使用するとエラー

でも、これはエラー

- from langchain_aws import ChatBedrock
+ from langchain_aws import ChatBedrockConverse
  from pydantic import BaseModel, Field
  
  
  class SearchQuery(BaseModel):
      search_query: str = Field(None, description="ウェブ検索に最適化されたクエリ")
  
- llm = ChatBedrock(model="anthropic.claude-3-haiku-20240307-v1:0")
+ llm = ChatBedrockConverse(model="anthropic.claude-3-haiku-20240307-v1:0")
  llm = llm.with_structured_output(SearchQuery)
  
  response = llm.invoke("Googleで'富士山'を検索")
  
  print(response.search_query)

botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid length for parameter toolConfig.tools[0].toolSpec.description, value: 0, valid min length: 1

descriptionの最小文字数が1以上ですが、上記実装では""(空文字)がセットされてしまいます。

image.png

対処法

docstringを追加することでdescriptionにセットされます

  from langchain_aws import ChatBedrockConverse
  from pydantic import BaseModel, Field
  
  
  class SearchQuery(BaseModel):
+     """検索クエリー"""
      search_query: str = Field(None, description="ウェブ検索に最適化されたクエリ")
  
  
  llm = ChatBedrockConverse(model="anthropic.claude-3-haiku-20240307-v1:0")
  llm = llm.with_structured_output(SearchQuery)
  
  response = llm.invoke("Googleで'富士山'を検索")
  
  print(response.search_query)

富士山

めでたしめでたし

いやー、ハマった

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?