langchain/libs/core/langchain_core/runnables
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 infra: update mypy 1.10, ruff 0.5 (#23721) 2024-07-03 10:33:27 -07:00
base.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
branch.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
config.py core[patch]: Accept configurable keys top-level (#23806) 2024-07-20 03:49:00 +00:00
configurable.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
fallbacks.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
graph_ascii.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
graph_mermaid.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
graph_png.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
graph.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
history.py core[patch]: enable RunnableWithMessageHistory without config (#23775) 2024-07-22 10:36:53 -04:00
learnable.py [Enhancement] Add support for directly providing a run_id (#18990) 2024-03-18 15:03:04 -07:00
passthrough.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
retry.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
router.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00
schema.py Docs: Add how to dispatch custom callback events (#24278) 2024-07-16 17:38:32 -04:00
utils.py core[patch]: docstrings runnables update (#24161) 2024-07-12 11:27:06 -04:00