Skip to content

AnswerLikes.likedBy field validation error: Pydantic expects list but API returns null #47

@ddu4

Description

@ddu4

generated by Claude Sonnet 4

Environment

  • Glean Python SDK Version: glean-api-client==0.6.5 (Generated by Speakeasy)
  • Python Version: 3.10.14
  • Pydantic Version: 2.x
  • Operating System: macOS
  • Date Reported: June 14, 2025

Summary

The Glean Python SDK fails to parse chat API responses when answers have no likes, causing a Pydantic validation error. The AnswerLikes.liked_by field is defined as a required List[AnswerLike] but the API returns null when there are no likes.

Error Details

Full Error Message

1 validation error for Unmarshaller
body.messages.2.fragments.1.structuredResults.0.answer.likes.likedBy
  Input should be a valid list [type=list_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/list_type

Stack Trace

File "<shorten>/lib/python3.10/site-packages/glean/api_client/client_chat.py", line 139, in create
    return utils.unmarshal_json(http_res.text, models.ChatResponse)
File "<shorten>/lib/python3.10/site-packages/glean/api_client/utils/serializers.py", line 140, in unmarshal_json
    return unmarshal(from_json(raw), typ)
File "<shorten>/lib/python3.10/site-packages/glean/api_client/utils/serializers.py", line 150, in unmarshal
    m = unmarshaller(body=val)
File "<shorten>/lib/python3.10/site-packages/pydantic/main.py", line 253, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Unmarshaller

Reproduction Steps

  1. Use the Glean Python SDK to make a chat API call
  2. Ask a question that returns answers with zero likes
  3. The SDK fails to parse the response due to likedBy: null in the JSON

Minimal Reproduction Code

import os
from glean.api_client import Glean, models

def reproduce_bug():
    with Glean(
        api_token=os.getenv('GLEAN_API_KEY'),
        instance='<instance>',
    ) as client:
        res = client.client.chat.create(messages=[
            {
                "fragments": [
                    models.ChatMessageFragment(
                        text="What is invocation context in LinkedIn production system?",
                    ),
                ],
            },
        ], timeout_millis=30000)
        return res

# This will fail with the validation error
reproduce_bug()

Root Cause Analysis

Problem Location

File: glean/api_client/models/answerlikes.py
Line: ~22

Current (Incorrect) Definition

class AnswerLikes(BaseModel):
    liked_by: Annotated[List["AnswerLike"], pydantic.Field(alias="likedBy")]
    # ... other fields

API Behavior vs Schema Mismatch

  • Schema Expectation: likedBy is a required array of AnswerLike objects
  • Actual API Response: likedBy can be null when no likes exist
  • Pydantic Validation: Fails because null is not a valid List

Proposed Solutions

Option 1: Make Field Optional (Recommended)

class AnswerLikes(BaseModel):
    liked_by: Annotated[Optional[List["AnswerLike"]], pydantic.Field(alias="likedBy", default_factory=list)]
    # ... other fields

Option 2: Update API to Return Empty Array

Change the API to return "likedBy": [] instead of "likedBy": null

Option 3: Update OpenAPI Specification

If this is generated from an OpenAPI spec, update the specification to mark likedBy as nullable/optional.

Temporary Workaround Applied

We temporarily modified the generated SDK file with Option 1 fix:

# Added Optional import
from typing import List, TYPE_CHECKING, Optional

# Modified field definition
liked_by: Annotated[Optional[List["AnswerLike"]], pydantic.Field(alias="likedBy", default_factory=list)]

This workaround works but will be overwritten when the SDK is regenerated.

Impact

  • Severity: High - Breaks basic chat API functionality
  • Frequency: Common - Occurs whenever answers have no likes
  • Workaround Available: Yes, but requires manual SDK modification

Additional Context

  • The error occurs specifically in chat responses containing answer results
  • The AnswerLikesTypedDict in the same file also defines liked_by as required, suggesting this might be a specification issue
  • This affects any application using the Glean chat API that encounters answers with zero likes

SDK Generation Details

  • Generated by: Speakeasy (https://speakeasy.com)
  • File header indicates: "DO NOT EDIT" - confirming this is auto-generated
  • Suggests the fix should be in the source specification or generation logic

Request

Please fix this by either:

  1. Updating the OpenAPI specification to mark likedBy as optional/nullable
  2. Updating the API implementation to return empty arrays instead of null
  3. Updating the SDK generation logic to handle nullable arrays properly

This issue prevents normal usage of the Glean chat API in production environments.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions