Skip to content

Native json output support for agents #118

@rashadism

Description

@rashadism

Description

I've been playing around with fantasy recently and really loving it!

Is there currently a way to get structured output from agents similar to object.Generate[T] for models? I couldn't find one in the docs or code.

I got it working with a tool + HasToolCall stop condition, but couldn't figure out how to get json mode working with agents.

If it is not supported yet, I'd suggest something like follows:

Current interfaces

  type LanguageModel interface {
      Generate(context.Context, Call) (*Response, error)
      Stream(context.Context, Call) (StreamResponse, error)
      GenerateObject(context.Context, ObjectCall) (*ObjectResponse, error)
      StreamObject(context.Context, ObjectCall) (ObjectStreamResponse, error)
      Provider() string
      Model() string
  }

  type Agent interface {
      Generate(context.Context, AgentCall) (*AgentResult, error)
      Stream(context.Context, AgentStreamCall) (*AgentResult, error)
  }

Suggestion: Add ObjectGenerator interface

  type ObjectGenerator interface {
      GenerateObject(context.Context, ObjectCall) (*ObjectResponse, error)
      StreamObject(context.Context, ObjectCall) (ObjectStreamResponse, error)
  }

Have Agent implement this interface. The agent would resolve the mode from its underlying model's provider(Or should this be configurable for both agents and models?)

This would allow object.Generate[T] to work seamlessly with both agents and models

  // Works with model
  result, _ := object.Generate[Analysis](ctx, model, fantasy.ObjectCall{...})

  // Same API works with agent  
  result, _ := object.Generate[Analysis](ctx, agent, fantasy.ObjectCall{...})

Would love to hear on this, and will be happy to send a PR if this looks good and aligns with the project direction

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions