# Cant seem to solve the problem

**URL:** <https://forum.codecrafters.io/t/cant-seem-to-solve-the-problem/5799>\
**Category:** Challenges\
**Tags:** challenge:kafka\
**Created:** [March 1, 2025, 7:47pm UTC](https://forum.codecrafters.io/t/cant-seem-to-solve-the-problem/5799 "2025-03-01T19:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GluStu](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/glustu/32/4770_2.png) [@GluStu](https://forum.codecrafters.io/u/GluStu)\
**Post date:** [March 1, 2025, 7:47pm UTC](https://forum.codecrafters.io/t/cant-seem-to-solve-the-problem/5799/1 "2025-03-01T19:47:46Z")

</div>

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:

 ![Screenshot 2025-03-02 011118](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/b/b1e872e92eb0d4a6ed088b029b3135a2a9589d27.png)

My code:

```auto
   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

```

---

<div class="post-metadata">

**Author:** ![yanruijie902136](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/yanruijie902136/32/2154_2.png) [@yanruijie902136](https://forum.codecrafters.io/u/yanruijie902136)\
**Post date:** [March 2, 2025, 5:48am UTC](https://forum.codecrafters.io/t/cant-seem-to-solve-the-problem/5799/3 "2025-03-02T05:48:55Z")

</div>

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](https://kafka.apache.org/protocol.html#protocol_types) and [ApiVersions response version 4](https://kafka.apache.org/protocol.html#The_Messages_ApiVersions). These two sections will be enough for you to fix the problem.

---

<div class="post-metadata">

**Author:** ![GluStu](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/glustu/32/4770_2.png) [@GluStu](https://forum.codecrafters.io/u/GluStu)\
**Post date:** [March 2, 2025, 2:37pm UTC](https://forum.codecrafters.io/t/cant-seem-to-solve-the-problem/5799/4 "2025-03-02T14:37:25Z")

</div>

Dude it worked. Test passed. Thank you sooo much 😊🫂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [March 7, 2025, 2:48pm UTC](https://forum.codecrafters.io/t/cant-seem-to-solve-the-problem/5799/5 "2025-03-07T14:48:17Z")

</div>

Closed on March 7, 2025, 2:48pm UTC

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