Skip to content

Agent with tools fails: "Object of type function is not JSON serializable" #3903

@pamelafox

Description

@pamelafox

Bug Report

Description

When using Agent with tools= and OpenAIChatClient, the agent fails with Object of type function is not JSON serializable on agent.run(). The raw Python function object is being included in the JSON payload sent to the OpenAI API instead of being converted to a tool/function schema.

This happens regardless of whether api_key is a string or callable.

Error

TypeError: Object of type function is not JSON serializable

Wrapped as:

agent_framework.exceptions.ServiceResponseException: <class 'agent_framework.openai._chat_client.OpenAIChatClient'> service failed to complete the prompt: Object of type function is not JSON serializable

Minimal Reproduction

import asyncio
from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient

client = OpenAIChatClient(
    api_key="your-key",
    model_id="gpt-5-mini",
)

def greet(name: str) -> str:
    """Say hello."""
    return f"Hello {name}"

agent = Agent(client=client, instructions="You are helpful.", tools=[greet])

async def main():
    r = await agent.run("greet Alice")  # <-- fails here
    print(r.text)

asyncio.run(main())

Observations

  • Fails with both string and callable api_key
  • Works when tools= is omitted (no tools)
  • The "function" in the error refers to the Python tool function object itself, not the API key
  • Something in the tool registration/serialization path is passing the raw callable into the request body instead of its JSON schema definition

Environment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions