Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object of type AgentAction is not JSON serializable #8815

Closed
3 of 14 tasks
coreygarvey opened this issue Aug 6, 2023 · 2 comments
Closed
3 of 14 tasks

Object of type AgentAction is not JSON serializable #8815

coreygarvey opened this issue Aug 6, 2023 · 2 comments
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@coreygarvey
Copy link

System Info

version 0.0.253

Running on a Jupyter Notebook in Google Colab

Who can help?

No response

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Follow the steps in Access Intermediate Steps within the Agent "How To".

The final step calls for
print(json.dumps(response["intermediate_steps"], indent=2))

This is throwing the following error:
TypeError: Object of type AgentAction is not JSON serializable

Based on this issue I think it may be happening following a recent upgrade.

Expected behavior

Printing the intermediate steps as JSON.

@dosubot dosubot bot added Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Aug 6, 2023
@dosubot
Copy link

dosubot bot commented Aug 6, 2023

Answer generated by a 🤖

Answer

Hello,

The error you're encountering, "TypeError: Object of type AgentAction is not JSON serializable", is due to the fact that Python's built-in json.dumps() function doesn't know how to serialize the AgentAction object. It only knows how to serialize basic data types like dict, list, str, int, float, bool, None.

However, LangChain provides a dumps function in the dump.py file that can be used to serialize objects. This function uses a default function that checks if the object is an instance of the Serializable class and if so, calls the to_json method of the object. If the object is not an instance of Serializable, it calls the to_json_not_implemented function.

You can modify your code as follows:

from langchain.load.dump import dumps

print(dumps(response["intermediate_steps"], pretty=True))

This code will convert the AgentAction object and any other objects in the intermediate_steps into a JSON format that can be printed. If the object is an instance of Serializable, its to_json method will be called. If not, the to_json_not_implemented function will be called.

You can find more information about this in the following sources:

Please try this solution and let me know if it resolves your issue or if you have any other questions.

Sources


This response is meant to be useful, save you time, and share context. 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.

@coreygarvey
Copy link
Author

Excellent! This worked perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant