crewai - ✅(Solved) Fix [BUG] TypeError: Object of type File is not JSON serializable when using input_files with gemma3 [2 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
crewAIInc/crewAI#4498Fetched 2026-04-08 00:41:45
View on GitHub
Comments
1
Participants
2
Timeline
9
Reactions
0
Author
Timeline (top)
cross-referenced ×2labeled ×2referenced ×2comment_deleted ×1

When using the crewai_files module to pass File objects into crew.kickoff(input_files=...), the execution fails during the LLM call. The underlying httpx client attempts to serialize the File object into a JSON payload for the OpenAI-compatible endpoint (Ollama/Gemma 3), leading to a TypeError.

Error Message

ERROR: OpenAI API call failed: Object of type File is not JSON serializable ERROR: OpenAI API call failed: Object of type File is not JSON serializable

[CrewAIEventsBus] Warning: Event pairing mismatch. 'llm_call_failed' closed 'agent_execution_started' (expected 'llm_call_started') An unknown error occurred. Please check the details below. Error details: Object of type File is not JSON serializable An unknown error occurred. Please check the details below. Error details: Object of type File is not JSON serializable [CrewAIEventsBus] Warning: Event pairing mismatch. 'agent_execution_error' closed 'task_started' (expected 'agent_execution_started') [CrewAIEventsBus] Warning: Event pairing mismatch. 'task_failed' closed 'crew_kickoff_started' (expected 'task_started') [CrewAIEventsBus] Warning: Ending event 'crew_kickoff_failed' emitted with empty scope stack. Missing starting event? ERROR: Programmatic analysis failed for 138/2026 Traceback (most recent call last): File "/.../project/src/ai_albo_bot/telegram/notifier.py", line 207, in notify_users res = analyze.analyze_item(entry) File "/.../project/src/ai_albo_bot/agent/analyze.py", line 137, in analyze_item crew_result = crew.kickoff(input_files=input_files) File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 743, in kickoff result = self._run_sequential_process() File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 1150, in _run_sequential_process return self._execute_tasks(self.tasks) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^ File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 1236, in _execute_tasks task_output = task.execute_sync( agent=exec_data.agent, context=context, tools=exec_data.tools, ) File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 499, in execute_sync return self._execute_core(agent, context, tools) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 740, in _execute_core raise e # Re-raise the exception after emitting the event ^^^^^^^ File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 671, in _execute_core result = agent.execute_task( task=self, context=context, tools=tools, ) File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 493, in execute_task result = self.execute_task(task, context, tools) File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 493, in execute_task result = self.execute_task(task, context, tools) File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 492, in execute_task raise e File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 459, in execute_task result = self._execute_without_timeout(task_prompt, task) File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 568, in _execute_without_timeout return self.agent_executor.invoke( ~~~~~~~~~~~~~~~~~~~~~~~~~~^ { ^ ...<4 lines>... } ^ )["output"] ^ File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 207, in invoke formatted_answer = self._invoke_loop() File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 305, in _invoke_loop return self._invoke_loop_native_tools() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 579, in _invoke_loop_native_tools raise e File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 498, in _invoke_loop_native_tools answer = get_llm_response( llm=self.llm, ...<9 lines>... verbose=self.agent.verbose, ) File "/.../project/.venv/lib/python3.13/site-packages/crewai/utilities/agent_utils.py", line 372, in get_llm_response raise e File "/.../project/.venv/lib/python3.13/site-packages/crewai/utilities/agent_utils.py", line 362, in get_llm_response answer = llm.call( messages, ...<5 lines>... response_model=response_model, ) File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 410, in call return self._call_completions( ~~~~~~~~~~~~~~~~~~~~~~^ messages=formatted_messages, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<4 lines>... response_model=response_model, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 450, in _call_completions return self._handle_completion( ~~~~~~~~~~~~~~~~~~~~~~~^ params=completion_params, ^^^^^^^^^^^^^^^^^^^^^^^^^ ...<3 lines>... response_model=response_model, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 1687, in _handle_completion raise e from e File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 1585, in _handle_completion response: ChatCompletion = self.client.chat.completions.create(**params) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^ File "/.../project/.venv/lib/python3.13/site-packages/openai/_utils/_utils.py", line 287, in wrapper return func(*args, **kwargs) File "/.../project/.venv/lib/python3.13/site-packages/openai/resources/chat/completions/completions.py", line 925, in create return self._post( ~~~~~~~~~~^ "/chat/completions", ^^^^^^^^^^^^^^^^^^^^ ...<43 lines>... stream_cls=Stream[ChatCompletionChunk], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1242, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 958, in request request = self._build_request(options, retries_taken=retries_taken) File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 535, in _build_request return self._client.build_request( # pyright: ignore[reportUnknownMemberType] ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ headers=headers, ^^^^^^^^^^^^^^^^ ...<10 lines>... **kwargs, ^^^^^^^^^ ) ^ File "/.../project/.venv/lib/python3.13/site-packages/httpx/_client.py", line 378, in build_request return Request( method, ...<8 lines>... extensions=extensions, ) File "/.../project/.venv/lib/python3.13/site-packages/httpx/_models.py", line 408, in init headers, stream = encode_request( ~~~~~~~~~~~~~~^ content=content, ^^^^^^^^^^^^^^^^ ...<7 lines>... ), ^^ ) ^ File "/.../project/.venv/lib/python3.13/site-packages/httpx/_content.py", line 216, in encode_request return encode_json(json) File "/.../project/.venv/lib/python3.13/site-packages/httpx/_content.py", line 177, in encode_json body = json_dumps( ~~~~~~~~~~^ json, ensure_ascii=False, separators=(",", ":"), allow_nan=False ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ).encode("utf-8") ^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/init.py", line 242, in dumps **kw).encode(obj) ~~~~~~^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 202, in encode chunks = self.iterencode(o, _one_shot=True) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 263, in iterencode return _iterencode(o, 0) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 182, in default raise TypeError(f'Object of type {o.class.name} ' f'is not JSON serializable') TypeError: Object of type File is not JSON serializable

Root Cause

When using the crewai_files module to pass File objects into crew.kickoff(input_files=...), the execution fails during the LLM call. The underlying httpx client attempts to serialize the File object into a JSON payload for the OpenAI-compatible endpoint (Ollama/Gemma 3), leading to a TypeError.

Fix Action

Fixed

PR fix notes

PR #4499: Fix #4498: Strip File objects from messages when multimodal is unsupported

Description (problem / solution / changelog)

Fix #4498: Strip File objects from messages when multimodal is unsupported

Summary

When input_files containing File objects are passed to a model that doesn't support multimodal (e.g., gemma3 via Ollama), _process_message_files returned early without removing the files key from message dicts. These non-serializable File objects then leaked into the API payload, causing TypeError: Object of type File is not JSON serializable from httpx.

The fix ensures the files key is always stripped from every message, regardless of multimodal support. A warning is logged when files are dropped. The same fix is applied to three code paths:

  • BaseLLM._process_message_files (sync — native providers)
  • LLM._process_message_files (sync — LiteLLM wrapper)
  • LLM._aprocess_message_files (async — LiteLLM wrapper)

8 regression tests added covering BaseLLM, LiteLLM sync/async, OpenAI native, JSON serializability, multiple messages, and warning logging.

Review & Testing Checklist for Human

  • Verify the multimodal-supported path still works: pass input_files with an image to a vision model (e.g., gpt-4o) and confirm files are correctly converted to content blocks (not dropped). The tests only cover the unsupported path directly.
  • Check that the per-message warning log ("dropped from the request") won't be excessively noisy for users who pass many messages with files to non-multimodal models. Consider if it should log once instead.
  • Note that provider/api variable lookups moved inside the loop body — functionally equivalent but only evaluated for messages that actually have files.

Notes

Changed files

  • lib/crewai/src/crewai/llm.py (modified, +30/-8)
  • lib/crewai/src/crewai/llms/base_llm.py (modified, +16/-5)
  • lib/crewai/tests/llms/test_multimodal.py (modified, +169/-2)

PR #4536: fix: strip File objects from messages for non-multimodal LLMs

Description (problem / solution / changelog)

Summary

  • Fixes TypeError: Object of type File is not JSON serializable when using input_files with LLMs that don't support multimodal (e.g., Ollama/Gemma 3)
  • Root cause: BaseLLM._process_message_files() returned early for non-multimodal LLMs without removing the files key from messages, leaving non-serializable File objects in the message dicts that get passed to json.dumps() by the httpx client
  • Fix: always strip the files key from all messages when multimodal is not supported, before returning

Test plan

  • Added 5 unit tests in TestProcessMessageFiles class covering:
    • Non-multimodal LLM strips files key from messages
    • Messages are JSON-serializable after processing (reproduces the exact TypeError from the issue)
    • Messages without files key are unaffected
    • Multimodal LLM also strips files after converting to content blocks
    • Multimodal LLM properly converts files to provider content blocks

Fixes #4498

Changed files

  • lib/crewai/src/crewai/llms/base_llm.py (modified, +4/-0)
  • lib/crewai/tests/llms/test_multimodal.py (modified, +107/-1)

Code Example

ERROR: OpenAI API call failed: Object of type File is not JSON serializable
ERROR: OpenAI API call failed: Object of type File is not JSON serializable

[CrewAIEventsBus] Warning: Event pairing mismatch. 'llm_call_failed' closed 'agent_execution_started' (expected 'llm_call_started')
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable
[CrewAIEventsBus] Warning: Event pairing mismatch. 'agent_execution_error' closed 'task_started' (expected 
'agent_execution_started')
[CrewAIEventsBus] Warning: Event pairing mismatch. 'task_failed' closed 'crew_kickoff_started' (expected 'task_started')
[CrewAIEventsBus] Warning: Ending event 'crew_kickoff_failed' emitted with empty scope stack. Missing starting event?
ERROR: Programmatic analysis failed for 138/2026
Traceback (most recent call last):
  File "/.../project/src/ai_albo_bot/telegram/notifier.py", line 207, in notify_users
    res = analyze.analyze_item(entry)
  File "/.../project/src/ai_albo_bot/agent/analyze.py", line 137, in analyze_item
    crew_result = crew.kickoff(input_files=input_files)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 743, in kickoff
    result = self._run_sequential_process()
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 1150, in _run_sequential_process
    return self._execute_tasks(self.tasks)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 1236, in _execute_tasks
    task_output = task.execute_sync(
        agent=exec_data.agent,
        context=context,
        tools=exec_data.tools,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 499, in execute_sync
    return self._execute_core(agent, context, tools)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 740, in _execute_core
    raise e  # Re-raise the exception after emitting the event
    ^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 671, in _execute_core
    result = agent.execute_task(
        task=self,
        context=context,
        tools=tools,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 493, in execute_task
    result = self.execute_task(task, context, tools)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 493, in execute_task
    result = self.execute_task(task, context, tools)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 492, in execute_task
    raise e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 459, in execute_task
    result = self._execute_without_timeout(task_prompt, task)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 568, in _execute_without_timeout
    return self.agent_executor.invoke(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^
        {
        ^
    ...<4 lines>...
        }
        ^
    )["output"]
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 207, in invoke
    formatted_answer = self._invoke_loop()
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 305, in _invoke_loop
    return self._invoke_loop_native_tools()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 579, in _invoke_loop_native_tools
    raise e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 498, in _invoke_loop_native_tools
    answer = get_llm_response(
        llm=self.llm,
    ...<9 lines>...
        verbose=self.agent.verbose,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/utilities/agent_utils.py", line 372, in get_llm_response
    raise e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/utilities/agent_utils.py", line 362, in get_llm_response
    answer = llm.call(
        messages,
    ...<5 lines>...
        response_model=response_model,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 410, in call
    return self._call_completions(
           ~~~~~~~~~~~~~~~~~~~~~~^
        messages=formatted_messages,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        response_model=response_model,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 450, in _call_completions
    return self._handle_completion(
           ~~~~~~~~~~~~~~~~~~~~~~~^
        params=completion_params,
        ^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        response_model=response_model,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 1687, in _handle_completion
    raise e from e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 1585, in _handle_completion
    response: ChatCompletion = self.client.chat.completions.create(**params)
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_utils/_utils.py", line 287, in wrapper
    return func(*args, **kwargs)
  File "/.../project/.venv/lib/python3.13/site-packages/openai/resources/chat/completions/completions.py", line 925, in create
    return self._post(
           ~~~~~~~~~~^
        "/chat/completions",
        ^^^^^^^^^^^^^^^^^^^^
    ...<43 lines>...
        stream_cls=Stream[ChatCompletionChunk],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1242, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 958, in request
    request = self._build_request(options, retries_taken=retries_taken)
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 535, in _build_request
    return self._client.build_request(  # pyright: ignore[reportUnknownMemberType]
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        headers=headers,
        ^^^^^^^^^^^^^^^^
    ...<10 lines>...
        **kwargs,
        ^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_client.py", line 378, in build_request
    return Request(
        method,
    ...<8 lines>...
        extensions=extensions,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_models.py", line 408, in __init__
    headers, stream = encode_request(
                      ~~~~~~~~~~~~~~^
        content=content,
        ^^^^^^^^^^^^^^^^
    ...<7 lines>...
        ),
        ^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_content.py", line 216, in encode_request
    return encode_json(json)
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_content.py", line 177, in encode_json
    body = json_dumps(
           ~~~~~~~~~~^
        json, ensure_ascii=False, separators=(",", ":"), allow_nan=False
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ).encode("utf-8")
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 242, in dumps
    **kw).encode(obj)
          ~~~~~~^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 202, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 263, in iterencode
    return _iterencode(o, 0)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 182, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type File is not JSON serializable

---

INFO: OpenAI: Successfully validated tool 'read_file'
ERROR: OpenAI API call failed: Object of type File is not JSON serializable
ERROR: OpenAI API call failed: Object of type File is not JSON serializable
[CrewAIEventsBus] Warning: Event pairing mismatch. 'llm_call_failed' closed 'agent_execution_started' (expected 'llm_call_started')
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable
RAW_BUFFERClick to expand / collapse

Description

When using the crewai_files module to pass File objects into crew.kickoff(input_files=...), the execution fails during the LLM call. The underlying httpx client attempts to serialize the File object into a JSON payload for the OpenAI-compatible endpoint (Ollama/Gemma 3), leading to a TypeError.

Steps to Reproduce

1- Define a Crew with an agent using a local LLM (e.g. Ollama/Gemma 3). 2- Create a File object using crewai_files. 3- Call crew.kickoff(input_files={"my_file": File(source="path/to/file.pdf")}). 4- See error: TypeError: Object of type File is not JSON serializable.

Expected behavior

The File object should be correctly handled by the agent's executor.

Screenshots/Code snippets

ERROR: OpenAI API call failed: Object of type File is not JSON serializable
ERROR: OpenAI API call failed: Object of type File is not JSON serializable

[CrewAIEventsBus] Warning: Event pairing mismatch. 'llm_call_failed' closed 'agent_execution_started' (expected 'llm_call_started')
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable
[CrewAIEventsBus] Warning: Event pairing mismatch. 'agent_execution_error' closed 'task_started' (expected 
'agent_execution_started')
[CrewAIEventsBus] Warning: Event pairing mismatch. 'task_failed' closed 'crew_kickoff_started' (expected 'task_started')
[CrewAIEventsBus] Warning: Ending event 'crew_kickoff_failed' emitted with empty scope stack. Missing starting event?
ERROR: Programmatic analysis failed for 138/2026
Traceback (most recent call last):
  File "/.../project/src/ai_albo_bot/telegram/notifier.py", line 207, in notify_users
    res = analyze.analyze_item(entry)
  File "/.../project/src/ai_albo_bot/agent/analyze.py", line 137, in analyze_item
    crew_result = crew.kickoff(input_files=input_files)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 743, in kickoff
    result = self._run_sequential_process()
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 1150, in _run_sequential_process
    return self._execute_tasks(self.tasks)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/crew.py", line 1236, in _execute_tasks
    task_output = task.execute_sync(
        agent=exec_data.agent,
        context=context,
        tools=exec_data.tools,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 499, in execute_sync
    return self._execute_core(agent, context, tools)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 740, in _execute_core
    raise e  # Re-raise the exception after emitting the event
    ^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/task.py", line 671, in _execute_core
    result = agent.execute_task(
        task=self,
        context=context,
        tools=tools,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 493, in execute_task
    result = self.execute_task(task, context, tools)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 493, in execute_task
    result = self.execute_task(task, context, tools)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 492, in execute_task
    raise e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 459, in execute_task
    result = self._execute_without_timeout(task_prompt, task)
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agent/core.py", line 568, in _execute_without_timeout
    return self.agent_executor.invoke(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^
        {
        ^
    ...<4 lines>...
        }
        ^
    )["output"]
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 207, in invoke
    formatted_answer = self._invoke_loop()
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 305, in _invoke_loop
    return self._invoke_loop_native_tools()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 579, in _invoke_loop_native_tools
    raise e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/agents/crew_agent_executor.py", line 498, in _invoke_loop_native_tools
    answer = get_llm_response(
        llm=self.llm,
    ...<9 lines>...
        verbose=self.agent.verbose,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/utilities/agent_utils.py", line 372, in get_llm_response
    raise e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/utilities/agent_utils.py", line 362, in get_llm_response
    answer = llm.call(
        messages,
    ...<5 lines>...
        response_model=response_model,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 410, in call
    return self._call_completions(
           ~~~~~~~~~~~~~~~~~~~~~~^
        messages=formatted_messages,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        response_model=response_model,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 450, in _call_completions
    return self._handle_completion(
           ~~~~~~~~~~~~~~~~~~~~~~~^
        params=completion_params,
        ^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        response_model=response_model,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 1687, in _handle_completion
    raise e from e
  File "/.../project/.venv/lib/python3.13/site-packages/crewai/llms/providers/openai/completion.py", line 1585, in _handle_completion
    response: ChatCompletion = self.client.chat.completions.create(**params)
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_utils/_utils.py", line 287, in wrapper
    return func(*args, **kwargs)
  File "/.../project/.venv/lib/python3.13/site-packages/openai/resources/chat/completions/completions.py", line 925, in create
    return self._post(
           ~~~~~~~~~~^
        "/chat/completions",
        ^^^^^^^^^^^^^^^^^^^^
    ...<43 lines>...
        stream_cls=Stream[ChatCompletionChunk],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1242, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 958, in request
    request = self._build_request(options, retries_taken=retries_taken)
  File "/.../project/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 535, in _build_request
    return self._client.build_request(  # pyright: ignore[reportUnknownMemberType]
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        headers=headers,
        ^^^^^^^^^^^^^^^^
    ...<10 lines>...
        **kwargs,
        ^^^^^^^^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_client.py", line 378, in build_request
    return Request(
        method,
    ...<8 lines>...
        extensions=extensions,
    )
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_models.py", line 408, in __init__
    headers, stream = encode_request(
                      ~~~~~~~~~~~~~~^
        content=content,
        ^^^^^^^^^^^^^^^^
    ...<7 lines>...
        ),
        ^^
    )
    ^
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_content.py", line 216, in encode_request
    return encode_json(json)
  File "/.../project/.venv/lib/python3.13/site-packages/httpx/_content.py", line 177, in encode_json
    body = json_dumps(
           ~~~~~~~~~~^
        json, ensure_ascii=False, separators=(",", ":"), allow_nan=False
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ).encode("utf-8")
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 242, in dumps
    **kw).encode(obj)
          ~~~~~~^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 202, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 263, in iterencode
    return _iterencode(o, 0)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 182, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type File is not JSON serializable

Operating System

macOS 26

Python Version

3.13

crewAI Version

1.9.3

crewAI Tools Version

1.9.3

Virtual Environment

Venv

Evidence

INFO: OpenAI: Successfully validated tool 'read_file'
ERROR: OpenAI API call failed: Object of type File is not JSON serializable
ERROR: OpenAI API call failed: Object of type File is not JSON serializable
[CrewAIEventsBus] Warning: Event pairing mismatch. 'llm_call_failed' closed 'agent_execution_started' (expected 'llm_call_started')
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable
An unknown error occurred. Please check the details below.
Error details: Object of type File is not JSON serializable

Possible Solution

None

Additional context

LLM: gemma3 via Docker Model Runner

extent analysis

Fix Plan

To fix the TypeError: Object of type File is not JSON serializable error, we need to modify the crewai_files module to handle the File object correctly.

Here are the steps:

  • Modify the crew.kickoff function to accept a File object and read its contents before passing it to the httpx client.
  • Use the json module to serialize the file contents into a JSON payload.

Code Changes

import json

# Modify the crew.kickoff function to accept a File object
def kickoff(self, input_files):
    # Read the file contents
    file_contents = {}
    for file_name, file_object in input_files.items():
        with open(file_object.source, 'r') as file:
            file_contents[file_name] = file.read()
    
    # Serialize the file contents into a JSON payload
    json_payload = json.dumps(file_contents)
    
    # Pass the JSON payload to the httpx client
    response = self._call_completions(json_payload)
    return response

Alternatively, you can use the base64 module to encode the file contents into a base64 string, which can be serialized into a JSON payload.

import base64
import json

# Modify the crew.kickoff function to accept a File object
def kickoff(self, input_files):
    # Read the file contents and encode it into a base64 string
    file_contents = {}
    for file_name, file_object in input_files.items():
        with open(file_object.source, 'rb') as file:
            file_contents[file_name] = base64.b64encode(file.read()).decode('utf-8')
    
    # Serialize the file contents into a JSON payload
    json_payload = json.dumps(file_contents)
    
    # Pass the JSON payload to the httpx client
    response = self._call_completions(json_payload)
    return response

Verification

To verify that the fix worked, you can test the crew.kickoff function with a File object and check if the error is resolved. You can also use a debugger to step through the code and ensure that the file contents are being read and serialized correctly.

Extra Tips

  • Make sure to handle any exceptions that may occur when reading the file contents or serializing the JSON payload.
  • Consider adding error handling to the crew.kickoff function to handle cases where the file contents cannot be read or serialized.
  • If you are using a large file, consider using a streaming approach to read the file contents and serialize the JSON payload to avoid loading the entire file into memory.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

The File object should be correctly handled by the agent's executor.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

crewai - ✅(Solved) Fix [BUG] TypeError: Object of type File is not JSON serializable when using input_files with gemma3 [2 pull requests, 1 comments, 2 participants]