You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"What weighs more a pound of bricks or a pound of feathers"
1599
-
)
1597
+
structured_model.invoke(
1598
+
"What weighs more a pound of bricks or a pound of feathers"
1599
+
)
1600
1600
1601
-
# -> AnswerWithJustification(
1602
-
# answer='They weigh the same',
1603
-
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
1604
-
# )
1605
-
```
1601
+
# -> AnswerWithJustification(
1602
+
# answer='They weigh the same',
1603
+
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
1604
+
# )
1605
+
```
1606
1606
1607
-
Example: Pydantic schema (`include_raw=True`):
1607
+
??? example "Pydantic schema (`include_raw=True`)"
1608
1608
1609
-
```python
1610
-
from pydantic import BaseModel
1609
+
```python
1610
+
from pydantic import BaseModel
1611
1611
1612
1612
1613
-
class AnswerWithJustification(BaseModel):
1614
-
'''An answer to the user question along with justification for the answer.'''
1613
+
class AnswerWithJustification(BaseModel):
1614
+
'''An answer to the user question along with justification for the answer.'''
1615
1615
1616
-
answer: str
1617
-
justification: str
1616
+
answer: str
1617
+
justification: str
1618
1618
1619
1619
1620
-
model = ChatModel(model="model-name", temperature=0)
1621
-
structured_model = model.with_structured_output(
1622
-
AnswerWithJustification, include_raw=True
1623
-
)
1620
+
model = ChatModel(model="model-name", temperature=0)
1621
+
structured_model = model.with_structured_output(
1622
+
AnswerWithJustification, include_raw=True
1623
+
)
1624
1624
1625
-
structured_model.invoke(
1626
-
"What weighs more a pound of bricks or a pound of feathers"
1627
-
)
1628
-
# -> {
1629
-
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
1630
-
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
1631
-
# 'parsing_error': None
1632
-
# }
1633
-
```
1625
+
structured_model.invoke(
1626
+
"What weighs more a pound of bricks or a pound of feathers"
1627
+
)
1628
+
# -> {
1629
+
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
1630
+
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
1631
+
# 'parsing_error': None
1632
+
# }
1633
+
```
1634
1634
1635
-
Example: Dictionary schema (`include_raw=False`):
1635
+
??? example "Dictionary schema (`include_raw=False`)"
1636
1636
1637
-
```python
1638
-
from pydantic import BaseModel
1639
-
from langchain_core.utils.function_calling import convert_to_openai_tool
1637
+
```python
1638
+
from pydantic import BaseModel
1639
+
from langchain_core.utils.function_calling import convert_to_openai_tool
1640
1640
1641
1641
1642
-
class AnswerWithJustification(BaseModel):
1643
-
'''An answer to the user question along with justification for the answer.'''
1642
+
class AnswerWithJustification(BaseModel):
1643
+
'''An answer to the user question along with justification for the answer.'''
"What weighs more a pound of bricks or a pound of feathers"
1655
-
)
1656
-
# -> {
1657
-
# 'answer': 'They weigh the same',
1658
-
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
1659
-
# }
1660
-
```
1653
+
structured_model.invoke(
1654
+
"What weighs more a pound of bricks or a pound of feathers"
1655
+
)
1656
+
# -> {
1657
+
# 'answer': 'They weigh the same',
1658
+
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
1659
+
# }
1660
+
```
1661
1661
1662
1662
!!! warning "Behavior changed in `langchain-core` 0.2.26"
0 commit comments