Skip to content

Coercion error in extract_data when model returns empty values (nested type_array -> type_object -> type_integer) #384

Closed

Description

@michael-dewar

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'

Created on 2025-03-27 with reprex v2.1.1.9000

Expected Outcome

I would expect to get an R object with NA_integer or NA_real_ in place of the missing values.

Activity

  1. hadley commented on Mar 26, 2025

    @hadley
    Member

    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.

  2. michael-dewar commented on Mar 27, 2025

    @michael-dewar
    Author

    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.

  3. hadley commented on Mar 27, 2025

    @hadley
    Member
  4. added this to the 0.2.0 milestone on Mar 27, 2025
  5. added
    bugan unexpected problem or unintended behavior
    on Mar 27, 2025
  6. added a commit that references this issue on Mar 27, 2025
    838c936
  7. added a commit that references this issue on Apr 1, 2025
    4780a4e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions