Skip to content

How do I load back the tool call and its outputs if I am locally managing the history ? #2253

@HailTheLotusJewel

Description

@HailTheLotusJewel

Please read this first

  • Have you read the docs?Agents SDK docs: yes
  • Have you searched for related issues? Others may have had similar requests: yes

Question

Example case:

from agents import Agent, Runner, ToolOutputImage, function_tool
import asyncio
import json
import dotenv
dotenv.load_dotenv()

def save_to_file(filename, json_list):
    with open(filename, "w") as json_file:
        json.dump(json_list, json_file, indent=2)

@function_tool
def fetch_multiple_images() -> list[ToolOutputImage]:
    """Returns two different images for the AI to compare."""
    
    # Image 1: Golden Gate Bridge
    img1 = ToolOutputImage(
        image_url="https://foyrai-pixi.s3.amazonaws.com/outputs/720856df-9457-42b6-85cf-d7fcbb255fa24e42ed4e-5aac-4771-9808-c925fef7d661.webp",
        detail="auto"
    )
    
    # Image 2: Eiffel Tower
    img2 = ToolOutputImage(
        image_url="https://foyrai-pixi.s3.amazonaws.com/outputs/55fcc325-7e26-4fb8-989e-b596a7fd9fb73c1d54cf-2c7b-4b99-a72c-174326ef1f6a.webp",
        detail="auto"
    )

    # Return them as a list
    return [img1, img2]

async def main():
    agent = Agent(
        name="Assistant",
        instructions="You are a helpful assistant.",
        tools=[fetch_multiple_images],
    )

    result = await Runner.run(
        agent,
        input="hi, what can you do ? and also fetch me some images.",
    )
    
    print(result.final_output)
    print(result.raw_responses)
    history = result.to_input_list()
    save_to_file("history.json", history)

if __name__ == "__main__":
    asyncio.run(main())

Suppose I get this tool call history from the above file:

[...{
    "arguments": "{}",
    "call_id": "call_f2DRrPCketzMFeqbMouSc4Ha",
    "name": "fetch_multiple_images",
    "type": "function_call",
    "id": "fc_026f0126d3a925fc006957b38e93248193bffc6e2f10c6ea93",
    "status": "completed"
  },
  {
    "call_id": "call_f2DRrPCketzMFeqbMouSc4Ha",
    "output": [
      {
        "type": "input_image",
        "image_url": "https://foyrai-pixi.s3.amazonaws.com/outputs/720856df-9457-42b6-85cf-d7fcbb255fa24e42ed4e-5aac-4771-9808-c925fef7d661.webp",
        "detail": "auto"
      },
      {
        "type": "input_image",
        "image_url": "https://foyrai-pixi.s3.amazonaws.com/outputs/55fcc325-7e26-4fb8-989e-b596a7fd9fb73c1d54cf-2c7b-4b99-a72c-174326ef1f6a.webp",
        "detail": "auto"
      }
    ],
    "type": "function_call_output"
  }...]

Actual case:
Error:
Error code: 400 - {'error': {'message': "Invalid value: 'tool'. Supported values are: 'assistant', 'system', 'developer', and 'user'.", 'type': 'invalid_request_error', 'param': 'input[2]', 'code': 'invalid_value'}}

History:


[...{
      "role": "assistant",
      "content": "",
      "tool_calls": [
        {
          "id": "call_FsM8hSMoCoI7ekijMm9Yikyk",
          "type": "function",
          "function": {
            "name": "fetch_services",
            "arguments": "{args}"
          },
          "status": "completed"
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_FsM8hSMoCoI7ekijMm9Yikyk",
      "content": "stringified json output",
      "name": "fetch_services"
    },...]

My question is how do I properly store the tool output and load it back, so that I can seamlessly run this in a multi turn setting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions