Skip to content

get_token_cost() fails when API response lacks service_tier #903

@trangdata

Description

@trangdata

Problem

I'm using ellmer with my company's internal OpenAI endpoint.

This worked in ellmer v0.3.0:

library(ellmer)
chat <- chat_openai(
  model = "bedrock-anthropic-claude-3.7-sonnet",
  base_url = "internal-endpoint",
)
chat$chat("What is the difference between a tibble and a data frame?")

In ellmer v0.4.0, I'd start to get a response, then:

Error in data.frame(provider = provider@name, model = provider@model,
  arguments imply differing number of rows: 1, 0

Likely Cause

get_token_cost() is called with variant = NULL when the API response doesn't include service_tier. The data.frame() call then fails because variant has length 0 while other columns have length 1.

Potential Fix

Default variant to NULL and set to "" if so:

get_token_cost <- function(provider, tokens, variant = NULL) {
  variant <- variant %||% ""
  # ...
}

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