Is Bedrock not compatible with ainvoke? #1303
Replies: 1 comment
-
Simple fix: Instead of langchain's pydantic, use "from pydantic import BaseModel, Field" |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone, I am trying the example code https://github.com/langchain-ai/langgraph/blob/main/examples/plan-and-execute/plan-and-execute.ipynb for bedrock. However I found it not working for this following example:
`import operator
from typing import Annotated, List, Tuple, TypedDict
class PlanExecute(TypedDict):
input: str
plan: List[str]
past_steps: Annotated[List[Tuple], operator.add]
response: str`
`from langchain_core.pydantic_v1 import BaseModel, Field
class Plan(BaseModel):
"""Plan to follow in future"""
`from langchain_core.prompts import ChatPromptTemplate
from langchain_aws import ChatBedrockConverse
planner_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"""For the given objective, come up with a simple step by step plan.
This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps.
The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.""",
),
("placeholder", "{messages}"),
]
)
planner = planner_prompt | ChatBedrockConverse(
model="anthropic.claude-3-sonnet-20240229-v1:0",
temperature=0,
max_tokens=4096,
).with_structured_output(Plan)
planner.ainvoke(
{
"messages": [
("user", "what is the hometown of the current Australia open winner?")
]
}
)
`
I am getting the output as an object:
<coroutine object RunnableSequence.ainvoke at 0x332d83df0>
.Adding await infront gives the following error:
`ValidationError Traceback (most recent call last)
Cell In[50], line 1
----> 1 await planner.ainvoke(
2 {
3 "messages": [
4 ("user", "what is the hometown of the current Australia open winner?")
5 ]
6 }
7 )
File /opt/anaconda3/lib/python3.12/site-packages/langchain_core/runnables/base.py:2920, in RunnableSequence.ainvoke(self, input, config, **kwargs)
2918 part = functools.partial(step.ainvoke, input, config)
2919 if asyncio_accepts_context():
-> 2920 input = await asyncio.create_task(part(), context=context) # type: ignore
2921 else:
2922 input = await asyncio.create_task(part())
File /opt/anaconda3/lib/python3.12/site-packages/langchain_core/output_parsers/base.py:109, in BaseGenerationOutputParser.ainvoke(self, input, config, **kwargs)
102 async def ainvoke(
103 self,
104 input: Union[str, BaseMessage],
105 config: Optional[RunnableConfig] = None,
106 **kwargs: Optional[Any],
107 ) -> T:
108 if isinstance(input, BaseMessage):
--> 109 return await self._acall_with_config(
110 lambda inner_input: self.aparse_result(
111 [ChatGeneration(message=inner_input)]
112 ),
113 input,
114 config,
115 run_type="parser",
116 )
117 else:
118 return await self._acall_with_config(
119 lambda inner_input: self.aparse_result([Generation(text=inner_input)]),
120 input,
121 config,
122 run_type="parser",
123 )
File /opt/anaconda3/lib/python3.12/site-packages/langchain_core/runnables/base.py:1835, in Runnable._acall_with_config(self, func, input, config, run_type, **kwargs)
1831 coro = acall_func_with_variable_args(
1832 func, input, config, run_manager, **kwargs
1833 )
1834 if asyncio_accepts_context():
-> 1835 output: Output = await asyncio.create_task(coro, context=context) # type: ignore
1836 else:
1837 output = await coro
File /opt/anaconda3/lib/python3.12/site-packages/langchain_core/output_parsers/base.py:62, in BaseLLMOutputParser.aparse_result(self, result, partial)
48 async def aparse_result(
49 self, result: List[Generation], *, partial: bool = False
50 ) -> T:
51 """Async parse a list of candidate model Generations into a specific format.
52
53 Args:
(...)
60 Structured output.
61 """
---> 62 return await run_in_executor(None, self.parse_result, result)
File /opt/anaconda3/lib/python3.12/site-packages/langchain_core/runnables/config.py:619, in run_in_executor(executor_or_config, func, *args, **kwargs)
615 raise RuntimeError from exc
617 if executor_or_config is None or isinstance(executor_or_config, dict):
618 # Use default executor with context copied from current context
--> 619 return await asyncio.get_running_loop().run_in_executor(
620 None,
621 cast(Callable[..., T], partial(copy_context().run, wrapper)),
622 )
624 return await asyncio.get_running_loop().run_in_executor(executor_or_config, wrapper)
File /opt/anaconda3/lib/python3.12/concurrent/futures/thread.py:58, in _WorkItem.run(self)
55 return
57 try:
---> 58 result = self.fn(*self.args, **self.kwargs)
59 except BaseException as exc:
60 self.future.set_exception(exc)
File /opt/anaconda3/lib/python3.12/site-packages/langchain_core/runnables/config.py:610, in run_in_executor..wrapper()
608 def wrapper() -> T:
609 try:
--> 610 return func(*args, **kwargs)
611 except StopIteration as exc:
612 # StopIteration can't be set on an asyncio.Future
613 # it raises a TypeError and leaves the Future pending forever
614 # so we need to convert it to a RuntimeError
615 raise RuntimeError from exc
File /opt/anaconda3/lib/python3.12/site-packages/langchain_aws/function_calling.py:187, in ToolsOutputParser.parse_result(self, result, partial)
185 tool_calls: Any = result[0].message.tool_calls
186 if self.pydantic_schemas:
--> 187 tool_calls = [self._pydantic_parse(tc) for tc in tool_calls]
188 elif self.args_only:
189 tool_calls = [tc["args"] for tc in tool_calls]
File /opt/anaconda3/lib/python3.12/site-packages/langchain_aws/function_calling.py:202, in ToolsOutputParser.pydantic_parse(self, tool_call)
198 def pydantic_parse(self, tool_call: ToolCall) -> BaseModel:
199 cls = {schema.name: schema for schema in self.pydantic_schemas or []}[
200 tool_call["name"]
201 ]
--> 202 return cls(**tool_call["args"])
File /opt/anaconda3/lib/python3.12/site-packages/pydantic/v1/main.py:341, in BaseModel.init(pydantic_self, **data)
339 values, fields_set, validation_error = validate_model(pydantic_self.class, data)
340 if validation_error:
--> 341 raise validation_error
342 try:
343 object_setattr(pydantic_self, 'dict', values)
ValidationError: 1 validation error for Plan
steps
value is not a valid list (type=type_error.list)`
It works fine if I convert 'ainvoke' to 'invoke', how to make ainvoke work with ChatBedrockConverse?
Beta Was this translation helpful? Give feedback.
All reactions