Skip to content

validate_structured_result returns empty while usage shows there's an output (also it doesn't go to except) #896

Open
@seunggs

Description

@seunggs

I keep getting empty dict {} as validated_restructured_result response. Here's the code:

class DocumentModelResponse(TypedDict, total=False):
    model_response: str
    document_md: str

...

# Run agent with streaming
async with agent.run_stream(
    user_prompt=prompt, deps=agent_deps, message_history=message_history
) as result:
    # Stream the model's response with structured validation
    async for message, last in result.stream_structured(debounce_by=0.01):
        try:
            validated_response = await result.validate_structured_result(
                message,
                allow_partial=not last,
            )

            yield f"event: document_messages\ndata: {json.dumps(validated_response)}\n\n"
        except ValidationError as e:
            raise ModelRetry("some error message")

    # After streaming is complete, get usage stats
    usage = result.usage()
    logfire.info(
        "Received LLM response",
        **{
            "request_tokens": usage.request_tokens,
            "response_tokens": usage.response_tokens,
            "total_tokens": usage.total_tokens,
        },
    )
...

This is strange since usage log shows reponse_tokens which indicates that the model actually sent back a response:

{
  'request_tokens': 1076, 'response_tokens': 1039, 'total_tokens': 2115
}

It also doesn't fall back to except with ValidationError when empty dict is returned.

Any guidance on this would be much appreciated. Thanks!

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions