class_docstr: Stores an API request's inputs, outputs, and other metadata. Contains a method to make an API
  call.
class_docstr_tok: ["Stores","an","API","request","'s","inputs",",","outputs",",","and","other","metadata",".","Contains","a","method","to","make","an","API","call","."]
docstr: Converts LangChain objects to JSON-serializable formats.
docstr_tok: ["Converts","LangChain","objects","to","JSON-serializable","formats","."]
code: def _prepare_to_serialize(self, response: dict):
      """
      Converts LangChain objects to JSON-serializable formats.
      """
      from langchain.load.dump import dumps
  
      if "intermediate_steps" in response:
          steps = response["intermediate_steps"]
          if (
              isinstance(steps, tuple)
              and len(steps) == 2
              and isinstance(steps[0], AgentAction)
              and isinstance(steps[1], str)
          ):
              response["intermediate_steps"] = [
                  {
                      "tool": agent.tool,
                      "tool_input": agent.tool_input,
                      "log": agent.log,
                      "result": result,
                  }
                  for agent, result in response["intermediate_steps"]
              ]
          else:
              try:
                  # `AgentAction` objects are not yet implemented for serialization in `dumps`
                  # https://github.com/langchain-ai/langchain/issues/8815#issuecomment-1666763710
                  response["intermediate_steps"] = dumps(steps)
              except Exception as e:
                  _logger.warning(
                      f"Failed to serialize intermediate steps: {e!r}"
                  )
      # The `dumps` format for `Document` objects is noisy, so we will still have custom logic
      if "source_documents" in response:
          response["source_documents"] = [
              {
                  "page_content": doc.page_content,
                  "metadata": doc.metadata,
              }
              for doc in response["source_documents"]
          ]
  
imports: from __future__ import annotations
  import json
  import logging
  import queue
  import threading
  import time
  import traceback
  from concurrent.futures import ThreadPoolExecutor
  from dataclasses import dataclass
  from typing import Any, Dict, List, Literal, Optional, Set, Union
  import langchain.chains
  import pydantic
  from langchain.callbacks.base import BaseCallbackHandler
  from langchain.schema import AgentAction, AIMessage, HumanMessage, SystemMessage
  from langchain.schema import ChatMessage
  from packaging.version import Version
  import mlflow
  from mlflow.exceptions import MlflowException
label_desc: Use image node_id 1 for calling the APIRequest obj's underlying member method code with example usage: obj._prepare_to_serialize(response) without return types