langchain/libs/core/langchain_core/utils
Bagatur 236e957abb
core,groq,openai,mistralai,robocorp,fireworks,anthropic[patch]: Update BaseModel subclass and instance checks to handle both v1 and proper namespaces (#24417)
After this PR chat models will correctly handle pydantic 2 with
bind_tools and with_structured_output.


```python
import pydantic
print(pydantic.__version__)
```
2.8.2

```python
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

class Add(BaseModel):
    x: int
    y: int

model = ChatOpenAI().bind_tools([Add])
print(model.invoke('2 + 5').tool_calls)

model = ChatOpenAI().with_structured_output(Add)
print(type(model.invoke('2 + 5')))
```

```
[{'name': 'Add', 'args': {'x': 2, 'y': 5}, 'id': 'call_PNUFa4pdfNOYXxIMHc6ps2Do', 'type': 'tool_call'}]
<class '__main__.Add'>
```


```python
from langchain_openai import ChatOpenAI
from pydantic.v1 import BaseModel, Field

class Add(BaseModel):
    x: int
    y: int

model = ChatOpenAI().bind_tools([Add])
print(model.invoke('2 + 5').tool_calls)

model = ChatOpenAI().with_structured_output(Add)
print(type(model.invoke('2 + 5')))
```

```python
[{'name': 'Add', 'args': {'x': 2, 'y': 5}, 'id': 'call_hhiHYP441cp14TtrHKx3Upg0', 'type': 'tool_call'}]
<class '__main__.Add'>
```

Addresses issues: https://github.com/langchain-ai/langchain/issues/22782

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2024-07-22 20:07:39 +00:00
..
__init__.py core[minor],community[minor]: Upgrade all @root_validator() to @pre_init (#23841) 2024-07-08 16:09:29 -04:00
_merge.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
aiter.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
env.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
formatting.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
function_calling.py core,groq,openai,mistralai,robocorp,fireworks,anthropic[patch]: Update BaseModel subclass and instance checks to handle both v1 and proper namespaces (#24417) 2024-07-22 20:07:39 +00:00
html.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
image.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
input.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
interactive_env.py core[patch]: simple prompt pretty printing (#15968) 2024-01-12 21:08:51 -05:00
iter.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
json_schema.py core[patch]: Fix utils.json_schema.dereference_refs (#24335 KeyError: 400 in JSON schema processing) (#24337) 2024-07-19 13:31:00 -04:00
json.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
loading.py docs: Fix URL formatting in deprecation warnings (#23075) 2024-06-18 14:49:58 -04:00
mustache.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
pydantic.py core[minor]: Relax constraints on type checking for tools and parsers (#24459) 2024-07-19 21:47:34 -04:00
strings.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
utils.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00