Skip to content

Structured output - Claude's handling of optional required=FALSE types always returns a string if value is unknown #899

@thisisnic

Description

@thisisnic

Very cool how this is currently spoofed with tool calls :) Just found this when working on some course materials, though looks like it'll probably will be solved by #866.

library(ellmer)

colors <- type_string(required = FALSE)

chat <- chat_github()
chat$chat_structured("What's my favourite colour?""My favourite food is cake"type = colors)
#> Using model = "gpt-4.1".
#> NULL

chat <- chat_anthropic()
chat$chat_structured("What's my favourite colour?""My favourite food is cake"type = colors)
#> Using model = "claude-sonnet-4-5-20250929".
#> [1] "I don't have information about your favourite colour. You mentioned that your favourite food is cake, but you haven't told me what your favourite colour is. Would you like to tell me your favourite colour?"

It's a bit trickier when the type is, say, an integer not a string as it'll still return the string so breaks the type "guarantee" now.

library(ellmer)

fave_num <- type_integer(required = FALSE)

chat <- chat_anthropic()
#> Using model = "claude-sonnet-4-5-20250929".
chat$chat_structured("What's my favourite number?", "My favourite food is cake", type = fave_num)
#> [1] "<UNKNOWN>"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions