Skip to content

feat(core): decompose stringified property of JsonUtil.parse#531

Merged
samchon merged 5 commits intomainfrom
fix/json-parse-decompose
Mar 4, 2026
Merged

feat(core): decompose stringified property of JsonUtil.parse#531
samchon merged 5 commits intomainfrom
fix/json-parse-decompose

Conversation

@samchon
Copy link
Member

@samchon samchon commented Mar 4, 2026

This pull request introduces improvements to how JSON arguments are parsed and validated in the orchestration logic. The main change is that the parsing function now uses the operation's parameter schema to better handle cases where a property is a stringified object, ensuring more accurate deserialization.

Enhancements to JSON parsing and validation:

  • Updated JsonUtil.parse to accept an optional parameters argument (ILlmSchema.IParameters). If provided, the function checks if the parsed output contains a single property that should be an object (or a union including object/null), and attempts to parse it from a string if necessary. This helps handle cases where objects are serialized as strings in JSON arguments.
  • Added a new helper function decompose to JsonUtil.ts to perform the schema-aware deserialization logic described above.
  • Modified parseArguments in call.ts to pass the operation’s parameter schema to JsonUtil.parse, enabling the new behavior.

Type and import updates:

  • Added the ILlmSchema type and LlmTypeChecker import from @samchon/openapi to support schema-aware parsing.

@samchon samchon requested a review from sunrabbit123 March 4, 2026 14:42
@samchon samchon self-assigned this Mar 4, 2026
@samchon samchon added this to WrtnLabs Mar 4, 2026
@samchon samchon added the enhancement New feature or request label Mar 4, 2026
Copilot AI review requested due to automatic review settings March 4, 2026 14:42
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 4, 2026

Open in StackBlitz

@agentica/benchmark

npm i https://pkg.pr.new/@agentica/benchmark@531

@agentica/chat

npm i https://pkg.pr.new/@agentica/chat@531

agentica

npm i https://pkg.pr.new/agentica@531

@agentica/core

npm i https://pkg.pr.new/@agentica/core@531

create-agentica

npm i https://pkg.pr.new/create-agentica@531

@agentica/rpc

npm i https://pkg.pr.new/@agentica/rpc@531

@agentica/vector-selector

npm i https://pkg.pr.new/@agentica/vector-selector@531

commit: 005db2b

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates core orchestration JSON argument parsing to be schema-aware, specifically handling the case where an operation expects an object but the LLM returns that object as a stringified JSON value.

Changes:

  • Extended JsonUtil.parse to accept optional ILlmSchema.IParameters and added decompose to parse stringified object properties based on schema.
  • Updated parseArguments in orchestrate/call.ts to pass the operation’s parameter schema into JsonUtil.parse.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/core/src/utils/JsonUtil.ts Adds schema-aware post-processing to parse a stringified object property into an object.
packages/core/src/orchestrate/call.ts Passes the operation’s parameter schema into JsonUtil.parse during tool-call argument parsing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +321 to +323
const data: Record<string, unknown> = JsonUtil.parse(
toolCall.function.arguments,
operation.function.parameters,
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseArguments passes operation.function.parameters into JsonUtil.parse, but the tool schema actually sent to the LLM can be operation.function.separated.llm (see the tools: construction earlier in this file). For operations using separated.llm, this means JsonUtil.parse will use a different schema than the one that produced the arguments, so the new schema-aware decomposition can be skipped or applied incorrectly. Use the same parameter schema here as the one used when building the tool definition (i.e., prefer separated.llm when present, otherwise fall back to operation.function.parameters).

Suggested change
const data: Record<string, unknown> = JsonUtil.parse(
toolCall.function.arguments,
operation.function.parameters,
const parametersSchema =
operation.function.separated?.llm ?? operation.function.parameters;
const data: Record<string, unknown> = JsonUtil.parse(
toolCall.function.arguments,
parametersSchema,

Copilot uses AI. Check for mistakes.
Comment on lines +49 to +52
try {
output[key] = JSON.parse(value);
}
catch {}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decompose assigns output[key] = JSON.parse(value) for any valid JSON string, even if the parsed result is not an object (e.g. numbers, booleans, arrays, or null when the schema is object-only). Since this branch is meant to handle stringified objects, consider parsing into a temporary variable and only overwriting output[key] when the parsed value is actually an object (and null only if the schema union includes null). This avoids silently changing the argument type to another non-object value.

Suggested change
try {
output[key] = JSON.parse(value);
}
catch {}
let parsed: any;
try {
parsed = JSON.parse(value);
}
catch {
// If parsing fails, leave the original string value as-is.
return;
}
if (LlmTypeChecker.isAnyOf(schema)) {
// For anyOf where every member is object or null, accept object or null only.
if (parsed !== null && typeof parsed !== "object") {
return;
}
output[key] = parsed;
}
else {
// For plain object schema, only accept non-null objects.
if (parsed === null || typeof parsed !== "object") {
return;
}
output[key] = parsed;
}

Copilot uses AI. Check for mistakes.
@samchon samchon merged commit 4ed4b9b into main Mar 4, 2026
13 checks passed
@samchon samchon deleted the fix/json-parse-decompose branch March 4, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants