Skip to content

Add count-array extraction for counting tool calls in response arrays#290

Open
Kludex wants to merge 1 commit intoadd-web-search-request-pricingfrom
add-count-array-extraction
Open

Add count-array extraction for counting tool calls in response arrays#290
Kludex wants to merge 1 commit intoadd-web-search-request-pricingfrom
add-count-array-extraction

Conversation

@Kludex
Copy link
Copy Markdown
Member

@Kludex Kludex commented Feb 19, 2026

Summary

  • Adds a count field to UsageExtractorMapping that navigates from the response root to an array and counts items matching a condition
  • Enables extracting web_search and file_search call counts from OpenAI's Responses API output array, where tool calls appear as typed items rather than in the usage object
  • Makes path optional on UsageExtractorMapping - exactly one of path or count must be set

Test plan

  • Python tests for count extraction with web_search/file_search calls, zero matches, and absent output array
  • JS tests for count extraction with matching items, zero matches, and absent array
  • Existing extraction tests still pass
  • Price calculation with extracted tool use counts verified

Adds a `count` field to `UsageExtractorMapping` that navigates from
the response root to an array and counts items matching a condition.
This enables extracting web_search and file_search call counts from
OpenAI's Responses API output array, where tool calls appear as
typed items rather than in the usage object.
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View 4 additional findings in Devin Review.

Open in Devin Review

Comment on lines +207 to +216
for (const step of steps) {
if (typeof step === 'string') {
if (mappingCheck.guard(data)) {
data = data[step]
} else if (required) {
throw new Error(`Expected mapping at count path, got ${typeName(data)}`)
} else {
return null
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 JS countArrayItems silently ignores non-string (ArrayMatch) path steps

The countArrayItems function accepts count.path of type ExtractPath, which can contain ArrayMatch objects. However, the loop at lines 207-223 only navigates when typeof step === 'string' (line 208). When a step is an ArrayMatch object, the entire navigation for that step is skipped, but the undefined check at line 217 still runs against the unchanged data.

Root Cause and Impact

The if (typeof step === 'string') block (line 208) handles string steps, but there is no else branch handling ArrayMatch steps. The undefined check at line 217 is outside the string-check block, so it runs for every step — but when the step is an ArrayMatch, data was never modified, so it still holds the previous value.

For example, if count.path were ["output", {field: "type", match: ..., type: "array-match"}], the ArrayMatch step would be silently skipped, and the function would try to count items in the wrong data (the parent array instead of a matched sub-object).

The Python _count_array_items at packages/python/genai_prices/types.py:562-574 has the identical issue — only isinstance(step, str) is handled.

Currently this doesn't affect production because the YAML config only uses path: output (a simple string), but the type contract (ExtractPath) permits ArrayMatch in count paths, and the function will silently produce incorrect results for such inputs rather than raising an error.

Prompt for agents
In packages/js/src/extractUsage.ts, the countArrayItems function (lines 207-223) only handles string steps from the path but silently ignores ArrayMatch steps. Either add an else branch at line 216 that throws an error like `throw new Error('ArrayMatch steps are not supported in count paths')` when the step is not a string, or add proper handling for ArrayMatch objects similar to the extractPath function. The same fix should be applied to the Python _count_array_items function in packages/python/genai_prices/types.py at lines 562-574, adding handling for ArrayMatch steps (or raising ValueError for unsupported step types).
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant