lunary-ai / lunary-py

Lunary AI Python Client (Analytics, monitoring and evaluations for GenAI apps)
https://lunary.ai/docs/py
19 stars 15 forks source link

TypeError('Object of type AIMessage is not JSON serializable #38

Closed bsf-ks closed 5 months ago

bsf-ks commented 6 months ago

Exception: TypeError('Object of type AIMessage is not JSON serializable during sending events Lcel chain configuration: AgentExecutor agent tool Lunary version: 0.1.38 Langchain version: 0.1.6 Langchain-community: 0.0.19

Error in sending json:

{
      "event":"end",
      "type":"chain",
      ...
      "timestamp":"2024-02-28T11:02:02.704029+00:00",
      "message":"None",
      "input":"None",
      "output":{
         "lc":1,
         "type":"constructor",
         "id":[
            "langchain",
            "schema",
            "agent",
            "AgentFinish"
         ],
         "kwargs":{
            "return_values":{
               "output":"Test success !",
               "messages":[   <- not valid json
                  "AIMessage(content=""<FINAL_ANSWER>\nTest success !\n</FINAL_ANSWER>"")"
               ]
            },
            "log":"<FINAL_ANSWER>\nTest success !\n</FINAL_ANSWER>"
         }
      },
 ...
   }

When events stucks in EventQueue (because of error on sending events to self-hosted Lunary service) they are modified after by still running and streaming agent. Error is caused by messages are added at langchain\agents\agent_iterator.py in function _return/a_return

        returned_output["messages"] = output.messages

Solution: I think lunary.track_event should copy the output object to event making it read only

hughcrt commented 5 months ago

Fixed 🙂