Skip to content

[Microsoft.Extensions.AI] - Add new Structured output support for JSON in Ollama #5726

@pdevito3

Description

@pdevito3

Would love to see support for the new structured outputs that ollama supports in a new format option to describe the json schema object we want to enforce.

curl -X POST http://localhost:11434/api/chat -H "Content-Type: application/json" -d '{
  "model": "llama3.1",
  "messages": [{"role": "user", "content": "Tell me about Canada."}],
  "stream": false,
  "format": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string"
      },
      "capital": {
        "type": "string"
      },
      "languages": {
        "type": "array",
        "items": {
          "type": "string"
        }
      }
    },
    "required": [
      "name",
      "capital", 
      "languages"
    ]
  }
}'

As far as the API for this, I'd think we could just add a new Object option to ResponseFormat, but not sure exactly what i'd expect for the actual schema definition offhand?

new ChatOptions<MyExpectedShape>()
{
    ResponseFormat = ChatResponseFormat.Object,
    Schema = typeof(MyExpectedShape)
}

// ...

new ChatOptions()
{
    ResponseFormat = ChatResponseFormat.Object,
    Schema = typeof(MyExpectedShape),
    SchemaSerializerOptions = new JsonSerializerOptions
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
    }
}

regardless, this would greatly enhance the capabilities for json handling and would be a great value add!

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions