Cant seem to solve the problem

I’m stuck on Stage Include DescribeTopicPartitions in APIVersions (#yk1).

I’ve tried every possible argument but still got the same bytes error.
Can somebody please help me tell me what is wrong with my code it would be a great help, please.

logs:

My code:

   def make_response(request: KafkaRequest):
       response_header = request.correlation_id.to_bytes(4, byteorder='big')
    
    valid_api_versions = [0, 1, 2, 3, 4]
    error_code = (
        ErrorCode.NONE
        if request.api_version in valid_api_versions
        else ErrorCode.UNSUPPORTED_VERSION
    )

    throttle_time = 0
    api_versions = [
        (18, 0, 4),
        (75 , 0, 0)
    ]

    # Adding tagged fields (b'\x00' for no tagged fields)
    tag_buffer = b'\x00'

    response_body = (
        error_code.value.to_bytes(2, byteorder='big')+
        int(2).to_bytes(1, byteorder='big')
    )

    
    for api_key, min_ver, max_ver in api_versions:
        response_body += (
            request.api_key.to_bytes(2, byteorder='big') +
            min_ver.to_bytes(2, byteorder='big') +
            max_ver.to_bytes(2, byteorder='big')
        )
    

    response_body += (
        tag_buffer + 
        throttle_time.to_bytes(4, byteorder='big') +
        tag_buffer
        )

    # Calculate the full message length
    message_len = len(response_header) + len(response_body)


    # Return the complete response
    return message_len.to_bytes(4, byteorder='big') + response_header + response_body

Hi @GluStu, I think there are two problems with your code.

  1. api_versions should be encoded as a COMPACT_ARRAY. According to Kafka’s protocol guide:

First, the length N + 1 is given as an UNSIGNED_VARINT. Then N instances of type T follow.

Since your api_versions has two elements, you should use int(3).to_bytes(1, byteorder='big') instead of 2.

  1. Each element inside api_versions is encoded as follows:

error_code: INT16
min_version: INT16
max_version: INT16
TAG_BUFFER

You’re missing the TAG_BUFFER part in the for loop.

You can find details of Kafka’s binary protocol in primitive types and ApiVersions response version 4. These two sections will be enough for you to fix the problem.

2 Likes

Dude it worked. Test passed. Thank you sooo much :blush::people_hugging:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.