StudyVisuallyStart studying
Part 03 · Tool calling · Card 5 of 10

Arguments are a string

function.arguments is a JSON string, not an object. You must JSON.parse it, and it can be malformed or wrong.

Last updated

Analogy

Like req.body before body-parser: raw text that only looks like an object.

Code example

parse-args.tsTypeScript
const call = message.tool_calls[0];
const args = JSON.parse(call.function.arguments); // { id: "A12" }

In an agent

Parse and then validate (Zod) before running anything. The model can send wrong or malicious values.

Practice

Interview questions

  • How does function / tool calling work?
    • Describe each tool with a name, a description and a JSON schema for its parameters.
    • The model returns which tool to call and with what arguments. It never executes code itself.
    • Your code validates the arguments, runs the function, and sends the result back as a tool message.
    • The model then continues with the result in context.

Questions people ask

Why is function.arguments a string and not an object?

The model writes text, so the arguments arrive as a JSON string. You have to JSON.parse it yourself. It can be malformed or contain values that do not fit your rules, so wrap the parse in error handling and validate the result with a schema before running anything.

How should I validate tool arguments from the model?

Parse the JSON string, then check it with a Zod schema using safeParse. If it fails, do not run the tool. Return a clear error as the tool result, such as invalid prId, must be a positive integer. The model reads that message and usually fixes its arguments on the next step.