feat(py): stream typed chunks - #6013
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces typed streaming output support by enabling partial validation of accumulated streaming chunks against a Pydantic schema type. It updates ModelResponseChunk to handle partial validation, makes ActionRunContext generic over the chunk type, and refines type annotations across the codebase. Additionally, comprehensive tests are added to verify the partial validation behavior. The reviewer feedback suggests using getattr defensively when accessing schema_type on raw_request.output to prevent potential AttributeError exceptions.
| chunks = ChunkAccumulator( | ||
| message_index, | ||
| formatter, | ||
| schema_type=raw_request.output.schema_type if raw_request.output else None, |
There was a problem hiding this comment.
Accessing raw_request.output.schema_type directly can raise an AttributeError if schema_type is not a predefined field on GenerateActionOutputConfig (for example, when output_schema is None or a raw dictionary, and schema_type is never dynamically set).
Using getattr provides a safer, more defensive approach to avoid potential runtime crashes.
| schema_type=raw_request.output.schema_type if raw_request.output else None, | |
| schema_type=getattr(raw_request.output, 'schema_type', None) if raw_request.output else None, |
Experimental. Currently we stream untyped chunks (the content comes through as a dict rather than a validated model). Exploring parsing intermediate chunks into partials.