PIXELBANKv9.1.0
Menu

Coerce Tool Arguments to Declared Types

Problem Statement

LLMs emit tool arguments as strings. Coerce each argument to the type declared in the tool schema so the underlying function receives real values.

Background

A schema maps each parameter name to a type string: "integer", "number", "boolean", or "string". Booleans arrive as the strings "true"/"false" (case-insensitive). Arguments not present in the schema are passed through unchanged.

Your Task

Implement:

def coerce_args(args, schema):
  • args: dict of name -> string value.
  • schema: dict of name -> type string.
  • Return a new dict with each value coerced to its declared type.

Input Format

  • args (dict), schema (dict).

Output Format

  • A dict of coerced values.

Sample

print(coerce_args({"n": "5", "flag": "true"}, {"n": "integer", "flag": "boolean"}))

Output:

{'n': 5, 'flag': True}

Example:

Input:
print(coerce_args({"n": "5", "flag": "true"}, {"n": "integer", "flag": "boolean"}))
Output:
{'n': 5, 'flag': True}
Reasoning:
  • Iterate through the input arguments, starting with the key "n" which has the string value "5".
  • Look up "n" in the schema to find the declared type "integer", then coerce the string "5" into the integer 55.
  • Process the next argument, "flag", which holds the string value "true".
  • Check the schema for "flag" to identify the type "boolean", converting the string "true" (case-insensitive) into the boolean value True.
  • Since all arguments have been processed and coerced according to their schema definitions, the final output is {'n': 5, 'flag': True}.

Constraints:

  • Types: integer, number, boolean, string.
  • "true"/"false" are case-insensitive for booleans.
  • Args absent from the schema pass through unchanged.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Coerce Tool Arguments to Declared Types - Easy | PixelBank