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 5. - 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.
1. Background Knowledge
In tool use and function calling architectures, Large Language Models (LLMs) generate structured outputs, typically in JSON format, to invoke external tools. However, LLMs fundamentally operate on tokens and often emit all values as strings, regardless of the intended data type. For example, an integer argument like 5 might be generated as the string "5", and a boolean as "true".
The tool schema (often defined via JSON Schema) declares the expected type for each parameter. Common types include "integer", "number", "boolean", and "string". Type coercion is the process of converting a value from one type to another. In this context, it means parsing the string representation into the actual Python object required by the underlying function. This step is critical because passing a string "5" to a function expecting an int will cause a TypeError or produce incorrect results (e.g., string concatenation instead of arithmetic).
Understanding type systems in Python is essential. Python is dynamically typed, meaning variables do not have fixed types, but functions expect specific types for correct behavior. The int(), float(), and bool() constructors are the standard ways to coerce strings to these types. Note that bool("false") evaluates to True in Python because any non-empty string is truthy, so boolean coercion requires special handling.
2. Algorithm Approach
This problem follows a map/filter pattern over a dictionary. The algorithm is:
- Iterate over each key-value pair in the input args dictionary.
- For each key, check if it exists in the schema dictionary.
- If the key is in the schema, retrieve the declared type string.
- Based on the type string, apply the appropriate coercion function to the string value.
- If the key is not in the schema, pass the value through unchanged.
- Build and return a new dictionary with the coerced values.
This is a straightforward lookup-and-transform pattern. No complex data structures or recursion are needed.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.