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
print(coerce_args({"n": "5", "flag": "true"}, {"n": "integer", "flag": "boolean"})){'n': 5, 'flag': True}- 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 . - 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 valueTrue. - 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.