Object of type Document is not JSON serializable #13554
Replies: 5 comments 3 replies
-
🤖 Hello @jsvaldez829! I'm Dosu, a bot designed to assist you with troubleshooting, answering questions, and guiding you towards becoming a contributor. While we wait for a human maintainer, feel free to let me know how I can help you further. The error you're encountering, "TypeError: Object of type Document is not JSON serializable", suggests that you're trying to serialize a In the context of LangChain, a class Document(Serializable):
"""Class for storing a piece of text and associated metadata."""
page_content: str
"""String text."""
metadata: dict = Field(default_factory=dict)
"""Arbitrary metadata about the page content (e.g., source, relationships to other
documents, etc.).
"""
type: Literal["Document"] = "Document"
@classmethod
def is_lc_serializable(cls) -> bool:
"""Return whether this class is serializable."""
return True (Source: libs/langchain/langchain/schema/document.py) From the traceback you provided, it seems like the error is occurring when you're trying to send a To fix this issue, you need to convert the # Assuming `doc` is a Document object
json_serializable_doc = {
'page_content': doc.page_content,
'metadata': doc.metadata
} You can then send I hope this helps! Let me know if you have any other questions. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
Hello, I have the same code and I'm facing the same issue. However, I disagree with the solution proposed by DOSU because if I do it like this, it works: inputs = {"question": message.content}
result = await runnable.ainvoke(inputs)
msg = cl.Message(content=result["answer"], disable_feedback=True)
await msg.send() In other words, the response is in The error only occurs when I try to do the streaming. Thank you. |
Beta Was this translation helpful? Give feedback.
-
I ran into this today and found a hacky workaround for this: The reason this happens are actually three:
Anyhow the hacky workaround is:
class DocumentEncoder(json.JSONEncoder):
def __init__(self):
self.__dict__.update(json._default_encoder.__dict__)
self._default_encoder = json._default_encoder
def default(self, obj):
if isinstance(obj, Document):
return { # `obj.__dict__`, `Serializable.to_json` or similar could also work here
"page_content": obj.page_content,
"metadata": obj.metadata
}
return self._default_encoder.default(self, obj)
# Workaround chainlit/socketio serialization
json._default_encoder = DocumentEncoder() Keep in mind you should only do this once so you can use the same
# patch socketio packet json encoding so that json._default_encoder is used
sed -i "s/, separators=(',', ':'))/)/g" .venv/lib/python*/site-packages/socketio/packet.py This patching woulb be needed every time the library is updated unless you use a fork This is brittle so for a real solution libraries (including langchain) should be properly updated to allow users to provide |
Beta Was this translation helpful? Give feedback.
-
As above My solution is as follows: cb = cl.LangchainCallbackHandler()
async for chunk in chain.astream({"input": question, "chat_history": message_history.messages},
config=RunnableConfig(callbacks=[cb])):
if "answer" in chunk: # check if the chunk has an answer key
await msg.stream_token(chunk["answer"])
await msg.send() Hope this helped. I use ChatOllama in my project and its not the fastest. (it never was) NOTE if you are looking for performance this solutions might not be the best. |
Beta Was this translation helpful? Give feedback.
-
I have same error: from auth import CheckSession, Login, Logout, Signupfrom flask_cors import CORS app = Flask(name) Initializing necessary extensions extensionsmigrate = Migrate(app, db) Initializing the databasedb.init_app(app) File upload configurationsUPLOAD_FOLDER = 'uploads' Adds file upload configuration inside the app contextwith app.app_context():
Helper function to check allowed file extensionsdef allowed_file(filename): Helper function to check if a user is logged indef login_required(f): Token required decoratordef token_required(f):
Error handler for internal server errors@app.errorhandler(500) Routes Using RESTful API
class Welcome(Resource): Auth Routesclass Signup(Resource):
class Login(Resource):
class CheckSession(Resource):
class Logout(Resource): class User(Resource):
class Parent(Resource): class Notifications(Resource):@token_requireddef get(self):parent_id = session.get('parent_id')parent = Parent.query.get_or_404(parent_id)notifications = Notifications.query.filter_by(parent_id=parent.id).all()return jsonify([notification.to_dict() for notification in notifications]), 200class Student(Resource):@token_requireddef get(self):parent_id = session.get('parent_id')parent = Parent.query.get_or_404(parent_id)students = Student.query.filter_by(parent_id=parent.id).all()return jsonify([student.to_dict() for student in students]), 200
class Teacher(Resource):
class Student (Resource):
class LearningMaterial(Resource):
class LearningMaterialUpload(Resource):
class LearningMaterialDownload(Resource):
class Dashboard(Resource):@token_requireddef get(self):parent_id = session.get('parent_id')parent = Parent.query.get_or_404(parent_id)notifications = Notifications.query.filter_by(parent_id=parent.id).all()students = Student.query.filter_by(parent_id=parent.id).all()learning_materials = LearningMaterial.query.filter_by(parent_id=parent.id).all()return {'notifications': [notification.to_dict() for notification in notifications],'students': [student.to_dict() for student in students],'learning_materials': [learning_material.to_dict() for learning_material in learning_materials]}, 200
class Notifications(Resource):
class Class(Resource):
class RequestPasswordReset(Resource):
class PasswordResetConfirm(Resource):
Adding resources (endpoints)api.add_resource(Welcome, '/', '/welcome')
api.add_resource(RequestPasswordReset, '/password-reset-request')
api.add_resource(User, '/users', '/users/int:user_id') api.add_resource(Dashboard, '/dashboard')app execution pointif name == 'main': |
Beta Was this translation helpful? Give feedback.
-
Trying this chainlit code for a conversationalQAretrieval and getting this error:
This is the code:
Beta Was this translation helpful? Give feedback.
All reactions