I have an issue with code execution. I am using a fastAPI server with the python google-genai sdk. The app is designed to use code execution, with some use cases being to generate images and graphs. I followed the Google Colab linked in the docs, which fetches the data from an inline_data attribute, but on some graphs (I’m assuming due to larger file sizes) I get this error:
EXCEPTION in generate_stream: ValueError - Chunk too big
2025-08-09 14:55:12,974 ERROR root MainThread : Traceback (most recent call last):
File in generate_stream
async for chunk in stream:
File n async_generator
async for chunk in response: # type: ignore[attr-defined]
File "", , in async_generator
async for response in response_stream:
File "", line 1129, in async_generator
async for chunk in response:
File "", line 258, in __anext__
return await self.segment_iterator.__anext__()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 312, in async_segments
chunk = await self.response_stream.content.readline()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "project directory\venv\Lib\site-packages\aiohttp\streams.py", line 352, in readline
return await self.readuntil()
^^^^^^^^^^^^^^^^^^^^^^
File "venv\Lib\site-packages\aiohttp\streams.py", line 380, in readuntil
raise ValueError("Chunk too big")
ValueError: Chunk too big
I just wanted to ask if there was a way for me to customize the buffer through the API? Is this a Gemini API problem or do I have to go in and configure that for myself? Sorry if this is such a newbie question! All help is appreciated 
Hi @Firley_Company
The error is actually coming from the aiohttp library that the GenAI SDK uses under the hood for HTTP requests, not directly from the Gemini API itself.
Can you cehck once by increasing the aiohttp client’s buffer size?
1 Like
Hi Pannaga,
Thanks for your reply!
I realized that myself as well. The issue is the default SDK doesn’t allow the aihttp to just increase the buffer size to my knowledge and I had to use a custom streaming function so I could customize that, as shown here, but it feels like regular users who just want to use code execution for images shouldn’t have to jump through these hoops?
async def stream_gemini_manually(model_name: str, contents: List[Dict], api_key: str, system_instruction: str) -> AsyncGenerator[Dict, None]:
"""
Performs a manual streaming request to the Gemini API using aiohttp,
bypassing the genai library to gain full control over the buffer size.
"""
request_url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_name}:streamGenerateContent?alt=sse"
headers = {
"Content-Type": "application/json",
"x-goog-api-key": api_key,
}
payload = {
"contents": contents,
"system_instruction": {"parts": [{"text": system_instruction}]},
"tools": [{"code_execution": {}}]
}
# Create a session where we explicitly control the buffer size.
async with aiohttp.ClientSession(read_bufsize=25 * 1024 * 1024) as session:
try:
async with session.post(request_url, headers=headers, json=payload) as response:
response.raise_for_status()
async for line in response.content:
line_str = line.decode('utf-8').strip()
if line_str.startswith("data: "):
try:
yield json.loads(line_str[6:])
except json.JSONDecodeError:
logging.warning(f"Could not decode JSON chunk: {line_str[6:]}")
except aiohttp.ClientResponseError as e:
# The error details are in the exception's attributes, not a .text() method.
logging.error(f"HTTP Error during manual stream: {e.status} - {e.message} - Headers: {e.headers}")
yield {"error": f"HTTP {e.status}: {e.message}"}
except Exception as e:
logging.error(f"Unexpected error during manual Gemini stream: {e}")
yield {"error": f"An unexpected error occurred: {str(e)}"}
This is probably something to fix with the SDK itself? To allow the aiohttp customization? Or maybe I am doing something wrong. Regardless I appreciate the quick assistance! 
1 Like
got same for me when images are present in stream