DOCS: Fix Sample Code for Compatibility with Pydantic 2.0 (#13890)

- **Description:** 
I encountered an issue while running the existing sample code on the
page https://python.langchain.com/docs/modules/agents/how_to/agent_iter
in an environment with Pydantic 2.0 installed. The following error was
triggered:

```python
ValidationError                           Traceback (most recent call last)
<ipython-input-12-2ffff2c87e76> in <cell line: 43>()
     41 
     42 tools = [
---> 43     Tool(
     44         name="GetPrime",
     45         func=get_prime,

2 frames
/usr/local/lib/python3.10/dist-packages/pydantic/v1/main.py in __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 Tool
args_schema
  subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)
```

I have made modifications to the example code to ensure it functions
correctly in environments with Pydantic 2.0.
pull/13889/head^2
ggeutzzang 10 months ago committed by GitHub
parent 968ba6961f
commit f2af82058f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,7 +22,7 @@
"metadata": {},
"outputs": [],
"source": [
"import pydantic\n",
"from pydantic.v1 import BaseModel, Field\n",
"from langchain.agents import AgentType, initialize_agent\n",
"from langchain.agents.tools import Tool\n",
"from langchain.chains import LLMMathChain\n",
@ -65,12 +65,12 @@
"primes = {998: 7901, 999: 7907, 1000: 7919}\n",
"\n",
"\n",
"class CalculatorInput(pydantic.BaseModel):\n",
" question: str = pydantic.Field()\n",
"class CalculatorInput(BaseModel):\n",
" question: str = Field()\n",
"\n",
"\n",
"class PrimeInput(pydantic.BaseModel):\n",
" n: int = pydantic.Field()\n",
"class PrimeInput(BaseModel):\n",
" n: int = Field()\n",
"\n",
"\n",
"def is_prime(n: int) -> bool:\n",

Loading…
Cancel
Save