Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion provider/openaiprovider/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,18 @@ func buildCompletionParams(model string, messages []*message.Message, opts []age
switch frmt.Kind {
case "json":
if schema := frmt.Schema; schema != nil {
wireSchema := schema
if frmt.Strict {
var err error
wireSchema, err = strictSchemaToMap(schema)
if err != nil {
return openai.ChatCompletionNewParams{}, fmt.Errorf("failed to convert response format schema (type %T) to JSON format: %w", schema, err)
}
}
params.ResponseFormat.OfJSONSchema = &shared.ResponseFormatJSONSchemaParam{
JSONSchema: shared.ResponseFormatJSONSchemaJSONSchemaParam{
Name: frmt.Name,
Schema: schema,
Schema: wireSchema,
},
}
if desc := frmt.Description; desc != "" {
Expand Down
37 changes: 37 additions & 0 deletions provider/openaiprovider/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/microsoft/agent-framework-go/agent"
"github.com/microsoft/agent-framework-go/agent/format/jsonformat"
"github.com/microsoft/agent-framework-go/internal/agenttest"
"github.com/microsoft/agent-framework-go/internal/messagetest"
"github.com/microsoft/agent-framework-go/message"
Expand Down Expand Up @@ -109,6 +110,42 @@ func TestChatRequestIncludesAgentFrameworkUserAgent(t *testing.T) {
}
}

func TestChatResponseFormatSchemaConvertsJSONSchema(t *testing.T) {
type payload struct {
Name string `json:"name"`
Nickname string `json:"nickname,omitempty"`
}
format, err := jsonformat.For[payload]()
if err != nil {
t.Fatal(err)
}

const input = `
{
"messages":[{"role":"user","content":"hello"}],
"model":"gpt-4o-mini",
"response_format":{"type":"json_schema","json_schema":{"name":"payload","schema":{"properties":{"name":{"type":"string"},"nickname":{"type":"string"}},"type":"object","required":["name","nickname"],"additionalProperties":false},"strict":true}}
}
`
const output = `
{
"id":"chatcmpl-test",
"object":"chat.completion",
"created":1727888631,
"model":"gpt-4o-mini",
"choices":[{"index":0,"message":{"role":"assistant","content":"{\"name\":\"Ada\",\"nickname\":\"A\"}"},"finish_reason":"stop"}]
}
`

server := newTestServer(t, input, output)
defer server.Close()

a := newTestClient(server)
if _, err := a.RunText(t.Context(), "hello", agent.WithResponseFormat(format)).Collect(); err != nil {
t.Fatalf("error = %v", err)
}
}

func TestChatConfigInstructions_NonStreaming(t *testing.T) {
const input = `
{
Expand Down
8 changes: 7 additions & 1 deletion provider/openaiprovider/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,13 @@ func responsesBuildCompletionParams(config AgentConfig, messages []*message.Mess
switch frmt.Kind {
case "json":
if schema := frmt.Schema; schema != nil {
schemaMap, err := schemaToMap(schema)
var schemaMap map[string]any
var err error
if frmt.Strict {
schemaMap, err = strictSchemaToMap(schema)
} else {
schemaMap, err = schemaToMap(schema)
}
if err != nil {
return responses.ResponseNewParams{}, fmt.Errorf("failed to convert response format schema (type %T) to JSON format: %w", schema, err)
}
Expand Down
17 changes: 15 additions & 2 deletions provider/openaiprovider/responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,8 @@ data: {"type":"response.completed","sequence_number":2,"response":{"id":"resp_00

func TestResponsesResponseFormatSchemaConvertsJSONSchema(t *testing.T) {
type payload struct {
Name string `json:"name"`
Name string `json:"name"`
Nickname string `json:"nickname,omitempty"`
}
format, err := jsonformat.For[payload]()
if err != nil {
Expand All @@ -2654,7 +2655,7 @@ func TestResponsesResponseFormatSchemaConvertsJSONSchema(t *testing.T) {
{
"model":"gpt-4o-mini",
"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]}],
"text":{"format":{"type":"json_schema","name":"payload","schema":{"properties":{"name":{"type":"string"}},"type":"object","required":["name"],"additionalProperties":false},"strict":true}}
"text":{"format":{"type":"json_schema","name":"payload","schema":{"properties":{"name":{"type":"string"},"nickname":{"type":"string"}},"type":"object","required":["name","nickname"],"additionalProperties":false},"strict":true}}
}
`

Expand All @@ -2676,6 +2677,18 @@ func TestResponsesResponseFormatSchemaConvertsJSONSchema(t *testing.T) {
if _, err := a.RunText(t.Context(), "hello", agent.WithResponseFormat(format)).Collect(); err != nil {
t.Fatalf("error = %v", err)
}

localFormat, err := jsonformat.FromResponseFormat(format)
if err != nil {
t.Fatal(err)
}
data, err := localFormat.Marshal(payload{Name: "Ada"})
if err != nil {
t.Fatalf("local Marshal() rejected omitted optional property: %v", err)
}
if got, want := string(data), `{"name":"Ada"}`; got != want {
t.Fatalf("local Marshal() = %s, want %s", got, want)
}
}

func TestResponsesStreamingResponseWithQueuedUpdate_HandlesCorrectly(t *testing.T) {
Expand Down
Loading
Loading