Skip to content

Inline CSV uploads in code execution can reuse a stale file or corrupt raw bytes #8213

Description

@sumedh-aerram

Environment

ADK main (2.14.1), macOS, Python 3.11.17. Offline reproductions; no model, credentials, Docker, or network services needed. Regression introduction is unknown.

Symptoms and impact

With optimize_data_file=True, inline CSV attachments are extracted into executor input files. Two defects in the same extraction path can cause analysis of incorrect data:

  1. With include_contents="none", upload b"value\n10\n", finish the turn, then upload b"value\n99\n" at the same part position. Both requests derive data_1_1.csv. The second request reuses the first cached file and skips preprocessing because that name is already processed.
  2. Upload the valid one-cell CSV b"1234". The encoding helper mistakes these raw bytes for encoded content. The executor decodes them into b"\xd7m\xf8" instead of the original CSV.

Expected versus actual

Input Expected Actual on upstream/main
Raw inline CSV b"1234" Executor receives b"1234" Executor receives b"\xd7m\xf8"
Second current-turn CSV with value 99 Placeholder resolves to value 99; eligible for preprocessing Placeholder resolves to cached value 10; preprocessing skipped
Identical upload repeated at the same position Reuse its cached file without duplicates Reuses the filename regardless of content

Offline reproduction

Run the following script using Python 3.11 with dependencies installed and PYTHONPATH=<checkout>/src. It prints both symptoms before asserting the expected behavior. It fails on upstream/main and passes with the local fix.

"""Offline regression check: no model, credentials, or executor service."""
from google.genai import types
from google.adk.code_executors.code_executor_context import CodeExecutorContext
from google.adk.events.event import Event
from google.adk.flows.llm_flows.context._contents import _get_current_turn_contents
from google.adk.flows.llm_flows.extensions._code_execution import (
    _extract_and_replace_inline_files, get_content_as_bytes,
)
from google.adk.models.llm_request import LlmRequest


def upload(context, history, data):
    history.append(Event(author='user', content=types.Content(role='user', parts=[
        types.Part.from_bytes(data=data, mime_type='text/csv')
    ])))
    request = LlmRequest(contents=_get_current_turn_contents(None, history, 'agent'))
    files = _extract_and_replace_inline_files(context, request)
    placeholder = request.contents[0].parts[0].text
    current = next(f for f in files if f'`{f.name}`' in placeholder)
    history.append(Event(author='agent', content=types.Content(role='model', parts=[
        types.Part(text='Done')
    ])))
    return files, current


_, raw = upload(CodeExecutorContext({}), [], b'1234')
raw_bytes = get_content_as_bytes(raw.content)
context, history = CodeExecutorContext({}), []
_, first = upload(context, history, b'value\n10\n')
context.add_processed_file_names([first.name])
second_files, second = upload(context, history, b'value\n99\n')
repeated_files, repeated = upload(context, history, b'value\n99\n')
second_bytes = get_content_as_bytes(second.content)
new_files = [f for f in second_files if f.name not in context.get_processed_file_names()]
print('Raw upload decoded for executor:', repr(raw_bytes))
print('Second turn decoded for executor:', repr(second_bytes))
print('Cached files:', len(second_files), 'New files to preprocess:', len(new_files))
print('Identical re-upload adds files:', len(repeated_files) - len(second_files))
assert raw_bytes == b'1234'
assert second_bytes == b'value\n99\n'
assert len(second_files) == 2 and new_files == [second]
assert repeated_files == second_files and repeated == second
assert get_content_as_bytes(second_files[0].content) == b'value\n10\n'

The original independent reproductions are also recorded locally at out/reports/correctness-review-2/repro_csv_base64.py and out/reports/correctness-review-2/repro_csv_cache_collision.py. Those original scripts assert the buggy behavior and therefore intentionally fail once fixed.

Cause and proposed fix

_extract_and_replace_inline_files derives filenames from request-local indices but caches files across turns. Its filename-only check ignores newly uploaded bytes. Separately, get_encoded_file_content guesses whether bytes are encoded even though inline_data.data is known to be raw.

Encode raw inline bytes directly. Compare encoded content and MIME before reusing a positional filename; on collision, append a stable digest of MIME and content. Preserve earlier cached files and placeholders, and reuse the digest filename on identical re-upload. Leave the general encoding helper's existing behavior intact for compatibility.

Duplicate re-check

No matching issues or PRs updated since 2026-11-17 were returned by read-only searches for code execution CSV, inline_data csv, data file cache, get_encoded_file_content, base64 csv, and optimize_data_file. The earlier review separately distinguished historical PR #4024 (persisting replacements in history) from these new-upload collisions.

Activity

  1. added
    core
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

core

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions