BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Agents25 of 28 in topic

782. Parse a function call string and dispatch it with signature checks

MediumAgents Pro

Agents and simple command consoles receive calls as text, such as add(2, 3). Before running anything you must parse the text safely and check the arguments against the target function's signature.

Write call_from_string(registry, text). registry maps a name to a Python function. text should be a single call like name(1, 'a', k=[1, 2]) where every argument is a Python literal (numbers, strings, booleans, None, lists, tuples, dicts).

Return the function's result on success. Return the string 'error: parse' if the text is not a single call to a plain name with literal arguments, 'error: unknown function NAME' if the name is not in the registry, and 'error: bad arguments' if the arguments do not bind to the function's signature. Never use eval.

Functiondef call_from_string(registry, text)

Example 1

Input: with registry = {'add': lambda a, b=0: a + b}, call_from_string(registry, 'add(2, b=3)')

Output: 5 and call_from_string(registry, 'add(1, 2, 3)') returns 'error: bad arguments'

Example 2

Input: registry = R, text = 'add(2, b=3)'

Output: 5

Example 3

Input: registry = R, text = 'add(1, 2, 3)'

Output: 'error: bad arguments'

Constraints

  • Standard library only (ast, inspect).
  • Registry functions are pure and fast.
Hint 1

Use ast.parse with mode='eval'.

Hint 2

Use ast.literal_eval on each argument node.

Hint 3

Use inspect.signature(f).bind to check before calling.

Common mistakes
  • Using eval on the string.
  • Calling the function and catching TypeError, which may hide errors raised inside the function.
  • Allowing attribute calls like module.func.
Understand the idea

Turning text into a function call is where agents and consoles become unsafe if you are careless. eval would run anything. The safe route has three steps: parse the text into a syntax tree and check its shape (one call, plain name), convert only literal arguments with literal_eval, then check the arguments against the function's signature before calling it. For add(a, b=0), 'add(2, b=3)' binds fine and returns 5, while 'add(1, 2, 3)' fails to bind because there is one argument too many. Tool-calling agents do the same validation on JSON arguments before running a tool.

Topics
Agents
Similar questions
  • Write the loop that runs a tool-calling agentMedium
  • Retry a flaky tool with exponential backoffMedium
  • Extract a JSON tool call from messy model outputMedium
  • Enforce token and dollar budgets on an agent runMedium
All Agents problems
Report
782 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import ast, inspect
​
def call_from_string(registry, text):
# return the result or an 'error: ...' string
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS