backpack1098B
Convex Communityโ€ข2y agoโ€ข
1 reply
backpack1098

ConvexChatMessageHistory schema for langchain

Hi @michal, im reading the article: https://stack.convex.dev/ai-chat-using-langchain-and-convex and im trying to understand how the messages schema is constructured and its relationship with ConvexChatMessageHistory.

im currently using a RunnableSequence and it doesn't save to memory. i have to manually do
await memory.saveContext(
  {
    input: {
      sessionId,
      type: 'human',
      content: message
    }
  },
  {
    content: response.content
  }
)

however, im running into the error below
Uncaught Error: Uncaught Error: Failed to insert or update a document in table "messages" because it does not match the schema: Object contains extra field `session_id` that is not in the validator.
Path: .message.data
Object: {additional_kwargs: {}, content: "what is an avocado?", response_metadata: {}, session_id: "34a48a99-e910-43e9-af24-8a9dbfddcc79", type: "human"}
Validator: v.object({additional_kwargs: v.optional(v.any()), content: v.string(), name: v.optional(v.string()), response_metadata: v.optional(v.any()), role: v.optional(v.string())})

to fix this, i had to modify the schema to
@@ -36,11 +36,14 @@ export default defineSchema({
     sessionId: v.string(),
     message: v.object({
       type: v.string(),
       data: v.object({
+        type: v.optional(v.string()),
         content: v.string(),
         role: v.optional(v.string()),
         name: v.optional(v.string()),
         additional_kwargs: v.optional(v.any()),
+        session_id: v.optional(v.any()),
         response_metadata: v.optional(v.any())
       })
     })

any idea why the input is having a diff structure when it tries to write to the db? thank you ๐Ÿ™‚
Was this page helpful?