OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: [Python-SDK] TypeError: Object of type TextPart is not JSON serializable when calling client.add_message

Open ColdNSs opened this issue 3 months ago • 1 comments

Issue Origin

Observed or reproduced in a real environment

Bug Description

When using the OpenViking Python SDK (specifically the SyncHTTPClient), the documentation instructs developers to import and utilize structured message part classes (such as TextPart and ToolPart) to populate the parts parameter in add_message().

However, in openviking_sdk/client.py (inside the underlying add_message implementation), the SDK passes the parts list directly into the json parameter of the underlying httpx request without first serializing these custom objects. Because httpx uses the standard library's json.dumps()—which does not know how to serialize custom SDK dataclasses/models—this raises a TypeError and crashes the execution.

Steps to Reproduce

  1. Initialize the OpenViking local server.

  2. Initialize SyncHTTPClient and connect to the server.

  3. Call client.add_message using a list containing an instantiated TextPart or ToolPart object inside the parts argument.

Expected Behavior

The SDK should automatically serialize its own Part objects.

Actual Behavior

The SDK does not know how to serialize the Part objects and throws a TypeError.

Minimal Reproducible Example

import openviking as ov
from openviking_sdk import SyncHTTPClient
from openviking.message import TextPart

print(ov.__version__)

client = SyncHTTPClient(url="http://localhost:1933")
client.initialize()

session = client.create_session()
session_id = session.get("session_id")

client.add_message(
    session_id=session_id,
    role="user",
    parts=[TextPart(text="Hello wolrd!")]
)

Error Logs

Traceback (most recent call last):
  File "/Users/coldn/Developer/ttrpg/react_test/openviking_test.py", line 55, in <module>
    client.add_message(
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/openviking_sdk/client.py", line 2474, in add_message
    return run_async(
           ^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/openviking_sdk/_utils.py", line 67, in run_async
    raise value
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/openviking_sdk/_utils.py", line 16, in _capture_result
    return True, await coro
                 ^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/openviking_sdk/client.py", line 1496, in add_message
    response = await self._request(
               ^^^^^^^^^^^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/openviking_sdk/client.py", line 498, in _request
    response = await self._send_http_request(method, url, headers, request_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/openviking_sdk/client.py", line 475, in _send_http_request
    return await request_method(method, url, **call_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/httpx/_client.py", line 1527, in request
    request = self.build_request(
              ^^^^^^^^^^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/httpx/_client.py", line 378, in build_request
    return Request(
           ^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/httpx/_models.py", line 408, in __init__
    headers, stream = encode_request(
                      ^^^^^^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/httpx/_content.py", line 216, in encode_request
    return encode_json(json)
           ^^^^^^^^^^^^^^^^^
  File "/Users/coldn/Developer/ttrpg/react_test/.venv/lib/python3.12/site-packages/httpx/_content.py", line 177, in encode_json
    body = json_dumps(
           ^^^^^^^^^^^
  File "/Users/coldn/.local/share/uv/python/cpython-3.12.13-macos-aarch64-none/lib/python3.12/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/Users/coldn/.local/share/uv/python/cpython-3.12.13-macos-aarch64-none/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/coldn/.local/share/uv/python/cpython-3.12.13-macos-aarch64-none/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/Users/coldn/.local/share/uv/python/cpython-3.12.13-macos-aarch64-none/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type TextPart is not JSON serializable

OpenViking Version

0.4.13

Python Version

3.12.13

Operating System

macOS

Model Backend

None

Additional Context

No response

ColdNSs avatar Aug 08 '26 15:08 ColdNSs

Hi, I would like to work on this. I reproduced the reported TypeError on current main through both AsyncHTTPClient and SyncHTTPClient, and confirmed that the main-package compatibility client and batch_add_messages() share the same root cause. After my initial search I also found the author-closed, unmerged #3900; I will credit that earlier work and prepare a refreshed, comprehensive follow-up rather than simply duplicate it. The planned scope is standalone SDK + compatibility client, add + batch, all four Part types, exact nested image_url wire shape, raw-dict preservation, input non-mutation, and sync/async RED/GREEN tests. Happy to coordinate with @rocke2020 or adjust if maintainers prefer reviving #3900.

HaningZS avatar Aug 18 '26 20:08 HaningZS