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

未关闭
#8,213 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 3 天内回复

@llalitkumarrr 已经在做这个了。

开始于 2026年11月18日。

  • #8214 来自 @sumedh-aerram —— 未关闭

评估

这个 Issue 还没有评估数据。

描述

core

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.

主要语言
Python
星标
22.3k
派生
4.4k
平均合并
1 天 2 小时
30 天内合并 PR
6

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/adk-python 的其他 Issue

查看 google/adk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。