<@U080DD1FR4K> - In the morning we should talk abo...
# proj-sdk
s
@wide-doctor-37830 - In the morning we should talk about event serialization and you mentioned you wanted to make it more "Pydantic". It was not working in fastapi because fastapi uses pydantic type adapters under the hood - this is why I was getting this issue. This PR fixes it: https://github.com/All-Hands-AI/OpenHands-Server/pull/26 In essence, it builds a dynamic type at runtime which is the annotated tagged union of all event types with a discriminator based on class name. Admittedly, there is some "python dark magic" here - but If we were to port this back into agent sdk we would not need the TypeDescriminatorMixin, as long as we used code like:
Copy code
if TYPE_CHECKING:
    EventType = EventBase
else:
    EventType = get_serializable_polymorphic_type(EventBase)

event_type_adapter = TypeAdapter(EventType)  # where EventType is the tagged union
...
event_type_adapter.model_dump(intance)
event_type_adapter.model_validate(data)
Thinking about this, it may actually be clearer to create the discriminated union explicitly and then write a unit test that scans the agent sdk codebase loading all modules and making sure that each implementation of EventBase is in the union
Both solutions mean that we can't serialize locally defined classes or dynamically defined classes, but I think that's okay as the current solution will struggle with that anyway
c
Already have a proposal here
Dynamic defined class is only an issue with Action schema
We can rewrite action type based on proposal and get rid of discriminated union mixin and just write things out explicitly
I was planning on taking a stab here today, happy to do a quick huddle if yโ€™all have time to discuss
w
I can hop on a huddle in fifteen
๐Ÿ‘ 1
c
Sg!
w
@straight-sundown-84319 @chilly-france-44439 I'm around whenever if y'all want to hash out some serialization stuff on a quick huddle
c
๐Ÿ‘
@wide-doctor-37830
w
@chilly-france-44439 here's a sketch I was doing: check the SimpleType implementation as a way to serialize (some) Python types in Pydantic, and the Spec/SpecField class for a first pass at what might be needed. Don't think we should be using this for serializing the spec, but maybe it inspires a good Action/Observation implementation.
c
@straight-sundown-84319 for some reason there's an infinite recursion here.. did you see this yesterday? File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 559, in new_handler_func return js_modify_function(schema_or_field, current_handler) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/openhands/sdk/utils/discriminated_union.py", line 439, in __get_pydantic_json_schema__ sub_schema_json = handler(TypeAdapter(sub).core_schema) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 37, in call return self.handler(core_schema) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 495, in handler_func json_schema = generate_for_schema_type(schema_or_field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 1068, in function_after_schema return self.generate_inner(schema['schema']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 563, in generate_inner json_schema = current_handler(schema) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 37, in call return self.handler(core_schema) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 495, in handler_func json_schema = generate_for_schema_type(schema_or_field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 1488, in model_schema json_schema = self.generate_inner(schema['schema']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 563, in generate_inner json_schema = current_handler(schema) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 37, in call return self.handler(core_schema) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 495, in handler_func json_schema = generate_for_schema_type(schema_or_field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 1601, in model_fields_schema json_schema = self._named_required_fields_schema(named_required_fields) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 1392, in _named_required_fields_schema field_json_schema = self.generate_inner(field).copy() ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 563, in generate_inner json_schema = current_handler(schema) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 37, in call return self.handler(core_schema) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 495, in handler_func json_schema = generate_for_schema_type(schema_or_field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 1460, in model_field_schema return self.generate_inner(schema['schema']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 563, in generate_inner json_schema = current_handler(schema) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 37, in call return self.handler(core_schema) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 477, in handler_func if self.mode == 'serialization' and 'serialization' in schema_or_field: ^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 298, in mode if self._config.json_schema_mode_override is not None: ^^^^^^^^^^^^ File "/home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 294, in _config return self._config_wrapper_stack.tail ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RecursionError: maximum recursion depth exceeded /home/runner/work/agent-sdk/agent-sdk/.venv/lib/python3.12/site-packages/litellm/llms/custom_httpx/async_client_cleanup.py66 DeprecationWarning: There is no current event loop loop = asyncio.get_event_loop()
irrc it was working well in the server repo yesterday
s
I didn't see it before porting - maybe I was using an older version - but I do see it now - I'm working on it
๐Ÿ‘€ 1
c
I got the build binary executor working, will work on dockerization now (so we don't do repeated work) https://github.com/All-Hands-AI/agent-sdk/pull/320