Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions core/src/code_executors/code_execution_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,13 @@ export function convertCodeExecutionParts(
codeBlockDelimiter[1],
};
} else if (content.parts.length == 1 && lastPart.codeExecutionResult) {
content.parts[content.parts.length - 1] = {
text:
executionResultDelimiters[0] +
lastPart.codeExecutionResult.output +
executionResultDelimiters[1],
};
const output = lastPart.codeExecutionResult.output;
// No output means no delimiters either, just an empty text part.
const text =
output == null
? ''
: executionResultDelimiters[0] + output + executionResultDelimiters[1];
content.parts[content.parts.length - 1] = {text};
content.role = 'user';
}
}
44 changes: 44 additions & 0 deletions core/test/code_executors/code_execution_utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,50 @@ describe('convertCodeExecutionParts', () => {
expect(content.role).toBe('user');
});

it('converts a codeExecutionResult without output to an empty text part', () => {
const parts: Part[] = [
{codeExecutionResult: {outcome: Outcome.OUTCOME_OK}},
];
const content = {parts, role: 'model'};

convertCodeExecutionParts(content, CODE_DELIM, RESULT_DELIM);

// No output means no delimiters either - an empty text part, not
// '```tool_output\nundefined\n```'.
expect(content.parts[0].text).toBe('');
expect(content.parts[0].codeExecutionResult).toBeUndefined();
expect(content.role).toBe('user');
});

it('converts a codeExecutionResult with a null output to an empty text part', () => {
// An A2A data part or a persisted event is deserialized straight into the
// part, so an explicit null reaches the converter even though the SDK
// types output as string | undefined.
const nullOutput = {
codeExecutionResult: {outcome: Outcome.OUTCOME_OK, output: null},
} as unknown as Part;
const content = {parts: [nullOutput], role: 'model'};

convertCodeExecutionParts(content, CODE_DELIM, RESULT_DELIM);

expect(content.parts[0].text).toBe('');
expect(content.role).toBe('user');
});

it('keeps the delimiters when the output is an empty string', () => {
const parts: Part[] = [
{codeExecutionResult: {outcome: Outcome.OUTCOME_OK, output: ''}},
];
const content = {parts, role: 'model'};

convertCodeExecutionParts(content, CODE_DELIM, RESULT_DELIM);

// '' is not "no output": adk-python guards on `is not None`, so an empty
// result still renders as an empty tool_output block.
expect(content.parts[0].text).toBe('```tool_output\n\n```');
expect(content.role).toBe('user');
});

it('does not convert codeExecutionResult when there are multiple parts', () => {
const content = {
parts: [
Expand Down
Loading