langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
91.33k stars 14.53k forks source link

Object of type 'FieldInfo' is not JSON serializable #8448

Closed try-agaaain closed 1 year ago

try-agaaain commented 1 year ago

System Info

$ langchain.__version__
'0.0.234'
$ uname -a
Linux codespaces-92388d 5.15.0-1042-azure #49-Ubuntu SMP Tue Jul 11 17:28:46 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ python
Python 3.10.8 (main, Jun 15 2023, 01:39:58) [GCC 9.4.0] on linux

Who can help?

@hwchase17 @vowe

Information

Related Components

Reproduction

The project address is: https://github.com/eunomia-bpf/trace-agent. When uncomment the line # args_schema=AnalyseInput, in the file trace-agent/iminder/tools.py, and then run the project using python -m iminder pid, the following error occurs:

$ python -m iminder 480
Traceback (most recent call last):
  File "/usr/local/python/3.10.8/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/python/3.10.8/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/workspaces/trace-agent/iminder/__main__.py", line 11, in <module>
    bot.run([f"Obtain the resource usage of the process whose pid is {pid} over a period of time, "
  File "/workspaces/trace-agent/iminder/autogpt.py", line 61, in run
    return self.agent.run(tasks)
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/experimental/autonomous_agents/autogpt/agent.py", line 93, in run
    assistant_reply = self.chain.run(
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/chains/base.py", line 445, in run
    return self(kwargs, callbacks=callbacks, tags=tags, metadata=metadata)[
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/chains/base.py", line 243, in __call__
    raise e
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/chains/base.py", line 237, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/chains/llm.py", line 92, in _call
    response = self.generate([inputs], run_manager=run_manager)
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/chains/llm.py", line 101, in generate
    prompts, stop = self.prep_prompts(input_list, run_manager=run_manager)
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/chains/llm.py", line 135, in prep_prompts
    prompt = self.prompt.format_prompt(**selected_inputs)
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/prompts/chat.py", line 155, in format_prompt
    messages = self.format_messages(**kwargs)
  File "/workspaces/trace-agent/iminder/prompt.py", line 130, in format_messages
    misc_messages = self._format_misc_messages(**kwargs)
  File "/workspaces/trace-agent/iminder/prompt.py", line 67, in _format_misc_messages
    base_prompt = SystemMessage(content=self.construct_full_prompt(**kwargs))
  File "/workspaces/trace-agent/iminder/prompt.py", line 58, in construct_full_prompt
    full_prompt += f"\n\n{get_prompt(self.tools)}"
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/experimental/autonomous_agents/autogpt/prompt_generator.py", line 184, in get_prompt
    prompt_string = prompt_generator.generate_prompt_string()
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/experimental/autonomous_agents/autogpt/prompt_generator.py", line 113, in generate_prompt_string
    f"{self._generate_numbered_list(self.commands, item_type='command')}\n\n"
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/experimental/autonomous_agents/autogpt/prompt_generator.py", line 84, in _generate_numbered_list
    command_strings = [
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/experimental/autonomous_agents/autogpt/prompt_generator.py", line 85, in <listcomp>
    f"{i + 1}. {self._generate_command_string(item)}"
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/experimental/autonomous_agents/autogpt/prompt_generator.py", line 50, in _generate_command_string
    output += f", args json schema: {json.dumps(tool.args)}"
  File "/usr/local/python/3.10.8/lib/python3.10/site-packages/langchain/tools/base.py", line 418, in args
    return self.args_schema.schema()["properties"]
  File "pydantic/main.py", line 664, in pydantic.main.BaseModel.schema
  File "pydantic/schema.py", line 188, in pydantic.schema.model_schema
  File "pydantic/schema.py", line 582, in pydantic.schema.model_process_schema
  File "pydantic/schema.py", line 623, in pydantic.schema.model_type_schema
  File "pydantic/schema.py", line 249, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 217, in pydantic.schema.get_field_info_schema
  File "pydantic/schema.py", line 992, in pydantic.schema.encode_default
  File "pydantic/schema.py", line 991, in genexpr
  File "pydantic/schema.py", line 996, in pydantic.schema.encode_default
  File "pydantic/json.py", line 90, in pydantic.json.pydantic_encoder
TypeError: Object of type 'FieldInfo' is not JSON serializable

Expected behavior

In trace-agent/iminder/tools.py, I have defined two custom tools: one is called sample, and the other is called analyse_process. Both tools have only one input parameter, but of different types. sample takes an integer as input, while analyse_process takes a string. Strangely, sample works as expected, but analyse_process does not. My expectation was that both of them would function correctly.

yangmingzhe commented 1 year ago

There's an extra comma on line 118 of your code. It's causing the error you've come across.

class AnalyseInput(BaseModel): arg: str = Field( ..., description="Statistical data of process resource usage.", ), <--- here!

try-agaaain commented 1 year ago

Thank you very much, it was my carelessness.