charmbracelet / charmbracelet/fantasy

Native json output support for agents

Open
#118 2 comments 4 reactions 0 assignees Claimed by @crazybolillo View on GitHub
Dominant language
Go
Stars
987
Forks
137
Avg merge
14h 41m
Merged PRs (30d)
12

Description

### 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

```go
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

```go
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

```go
// 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

Contributor guide

Open the contributing guide

Research direction

Review the Agent and LanguageModel interfaces and the object.Generate[T] entry point described in the issue, then inspect linked pull request #192. Resolve how agents should expose structured generation and whether provider mode is configurable; done means the project has an agreed implementation path for using object.Generate with agents.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
ai, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.