Skip to content

Commit

Permalink
Add fallback for pydantic v1.
Browse files Browse the repository at this point in the history
  • Loading branch information
dokterbob committed Nov 19, 2024
1 parent 60cd178 commit 65f4b49
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion backend/chainlit/langchain/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from chainlit.context import context_var
from chainlit.message import Message
from chainlit.step import Step
import pydantic

DEFAULT_ANSWER_PREFIX_TOKENS = ["Final", "Answer", ":"]

Check failure on line 20 in backend/chainlit/langchain/callbacks.py

View workflow job for this annotation

GitHub Actions / lint-backend / lint-backend

Ruff (I001)

backend/chainlit/langchain/callbacks.py:1:1: I001 Import block is un-sorted or un-formatted

Expand Down Expand Up @@ -125,7 +126,13 @@ def ensure_values_serializable(self, data):
for key, value in data.items()
}
elif isinstance(data, BaseModel):
return data.model_dump()
# Fallback to support pydantic v1
# https://docs.pydantic.dev/latest/migration/#changes-to-pydanticbasemodel
if pydantic.VERSION.startswith("1"):
return data.dict()

# pydantic v2
return data.model_dump() # pyright: ignore reportAttributeAccessIssue
elif isinstance(data, list):
return [self.ensure_values_serializable(item) for item in data]
elif isinstance(data, (str, int, float, bool, type(None))):
Expand Down

0 comments on commit 65f4b49

Please sign in to comment.