You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Coercion error in extract_data when model returns empty values (nested type_array -> type_object -> type_integer) #384
I want to extract structured data in which some fields are not required. But when the AI response leaves them blank, I get a coercion error.
Reproducible Example
library(ellmer)
chat<- chat_openai(system_prompt="Extract the year of publication, if possible.",
model="gpt-4o-mini")
chat$extract_data(
c("Apples", "Oranges"),
type= type_array(items= type_object(
fruit= type_string(),
year= type_integer(required=FALSE)))
)
#> Error in convert_from_type(vals, type_array(items = type@items@properties[[name]])): 'list' object cannot be coerced to type 'integer'# The AI responded with empty 'year'chat#> <Chat turns=3 tokens=89/24>#> ── system ──────────────────────────────────────────────────────────────────────#> Extract the year of publication, if possible.#> ── user ────────────────────────────────────────────────────────────────────────#> Apples#> #> Oranges#> ── assistant ───────────────────────────────────────────────────────────────────#> [data] {#> "wrapper": [#> {#> "fruit": "Apples",#> "year": {}#> },#> {#> "fruit": "Oranges",#> "year": {}#> }#> ]#> }# I believe the problem is that the following returns NULL instead of NA_integer_chat$extract_data(
c("Apples", "Oranges"),
type= type_integer(required=FALSE))
#> NULL# Also, type_number() has an analogous problem:chat$extract_data(
c("Apples", "Oranges"),
type= type_array(items= type_object(
fruit= type_string(),
year= type_number(required=FALSE)))
)
#> Error in convert_from_type(vals, type_array(items = type@items@properties[[name]])): 'list' object cannot be coerced to type 'double'
Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.
Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.
I reworked the example above using reprex. Sorry for the trouble.
Activity
hadley commentedon Mar 26, 2025
Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.
michael-dewar commentedon Mar 27, 2025
I reworked the example above using
reprex. Sorry for the trouble.hadley commentedon Mar 27, 2025
@michael-dewar thanks!
Handle optional vector elements
Handle optional vector elements (tidyverse#392)