Skip to main content
By default the agent uses text-only output. Use WithResponseFormat to request structured output — for example JSON with a schema the model must follow.

Default (text)

Omit WithResponseFormat — the LLM responds as plain text:
a, err := agent.NewAgent(
    agent.WithLLMClient(llmClient),
    agent.WithSystemPrompt("You are a helpful assistant."),
    // No WithResponseFormat — text output
)
Force text explicitly when you need to reset a previously set format:
agent.WithResponseFormat(&interfaces.ResponseFormat{
    Type: interfaces.ResponseFormatText,
})

JSON with schema

Use interfaces.ResponseFormatJSON with a valid JSON Schema. The root must be type: "object" with properties:
import "github.com/agenticenv/agent-sdk-go/pkg/interfaces"

a, err := agent.NewAgent(
    agent.WithLLMClient(llmClient),
    agent.WithResponseFormat(&interfaces.ResponseFormat{
        Type: interfaces.ResponseFormatJSON,
        Name: "AgentResponse",
        Schema: interfaces.JSONSchema{
            "type": "object",
            "properties": interfaces.JSONSchema{
                "response":   interfaces.JSONSchema{"type": "string"},
                "confidence": interfaces.JSONSchema{"type": "number"},
            },
            "required": []any{"response"},
        },
    }),
)
The schema is sent to the LLM on every request for that agent. Parse result.Content as JSON in your application.
The schema root must be type: "object". Primitive or array roots are not valid JSON Schema for Structured Outputs.

Provider support

Structured Outputs (JSON schema enforcement) require models that support it — for example gpt-4o and gpt-4o-mini on OpenAI. Older models may fall back to JSON mode without strict schema enforcement.
Response format is part of the static agent spec. Include the same WithResponseFormat on both NewAgent and NewAgentWorker when using split processes — it participates in the fingerprint check.

Example

JSON Response

Structured output with response format

LLM Providers

OpenAI, Anthropic, and Gemini clients

Reasoning

Extended thinking alongside structured output

Configuration

WithResponseFormat reference

Tools

Combine structured output with tool use