# Describetopicpartion error

**URL:** <https://forum.codecrafters.io/t/describetopicpartion-error/8503>\
**Category:** Challenges\
**Tags:** challenge:kafka\
**Created:** [April 9, 2025, 6:03pm UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503 "2025-04-09T18:03:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![faith-in-het](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/faith-in-het/32/8759_2.png) [@faith-in-het](https://forum.codecrafters.io/u/faith-in-het)\
**Post date:** [April 9, 2025, 6:03pm UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/1 "2025-04-09T18:03:34Z")

</div>

I’m stuck on Stage #describetopicpartition stage

Here are my logs:

```auto
remote: [tester::#YK1] ✓ Correlation ID: 717410973
remote: [tester::#YK1] ✓ Error code: 0 (NO_ERROR)
remote: [tester::#YK1] Expected API keys array to include atleast 2 keys, got 1
remote: [tester::#YK1] Test failed

its not identifying 2nd key 

```

And here’s a snippet of my code:

````python
def handle_client(addr,port):
    try:
        while True:
           # a = 4 + 12 + 3
            a= 4 + 12 +3
            data = addr.recv(1024) ## each time loop iterates server waits for incoming data then only proceed 
            ##we used while True to allow continues processing of multiple requests without restarting the server
            if not data: # Client disconnected
                print(f"Client {port} disconnected")
                break

            corrId = getCorrId(data)
            errorCode = checkApiVersion(data)
            apiKey = data[4:6]
            api_keys_array = (
                        int(18).to_bytes(2, byteorder="big") # APIVersions key
                        + int(0).to_bytes(2, byteorder="big") # Min version
                        + int(4).to_bytes(2, byteorder="big") # Max version
                        + int(75).to_bytes(2, byteorder="big") # DescribeTopicPartitions key
                        + int(0).to_bytes(2, byteorder="big") # Min version
                        + int(0).to_bytes(2, byteorder="big"), # Max version
                    )

                # Combine the array of bytes into a single byte sequence
            api_keys_bytes = b"".join(api_keys_array)
            addr.sendall(
                a.to_bytes(4, byteorder="big")
                + corrId
                + errorCode
                + int(1 + 1).to_bytes(1, byteorder="big") #number of api keys
                + api_keys_bytes       
                + int(0).to_bytes(4, byteorder="big")#tag 
            )
    except Exception as e:
        print(f"Error handling client: {e}")
    finally:
        addr.close() ```
````

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [April 9, 2025, 7:08pm UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/3 "2025-04-09T19:08:35Z")

</div>

@faith-in-het, could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

---

<div class="post-metadata">

**Author:** ![faith-in-het](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/faith-in-het/32/8759_2.png) [@faith-in-het](https://forum.codecrafters.io/u/faith-in-het)\
**Post date:** [April 10, 2025, 6:41am UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/4 "2025-04-10T06:41:45Z")

</div>

> **[GitHub - faith-in-het/KAFKA](https://github.com/faith-in-het/KAFKA)**
>
> Contribute to faith-in-het/KAFKA development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [April 10, 2025, 8:48pm UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/6 "2025-04-10T20:48:46Z")

</div>

Hey @faith-in-het, there might be a few issues at play. Here are the first ones I noticed:

1. Incorrect **number** of API keys:

![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/optimized/2X/e/e8feacdf52acca8fdbad09210819b7d925628891_2_690x44.png)

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/a/a83aa56cdcdb75f6a72e32f37a943dc75f60f9c8.png)

1. Missing **TAG\_BUFFER** s (one byte of 0) for each api key:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/e/e18b092d98a18ac92a2a04bc12600510dba4a930.png)

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/5/5c5a11a4ddeca6fce23f489302b66c6aac18db21.png)

1. Missing **.throttle\_time\_ms**

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/b/b4be8c26b7fe5a2feb11f49a5dfc9cf2ceae228f.png)

1. Incorrect **message size** :

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/1/1f553888eb2f254d71027a3dec236ec546dde00d.png)

Feel free to use [BinspecVisualizer](https://binspec.org/kafka-api-versions-Response-v4?highlight=10-31) to view the structure of the APIVersions Response.

---

<div class="post-metadata">

**Author:** ![faith-in-het](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/faith-in-het/32/8759_2.png) [@faith-in-het](https://forum.codecrafters.io/u/faith-in-het)\
**Post date:** [April 11, 2025, 4:53am UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/8 "2025-04-11T04:53:38Z")

</div>

```auto
import socket # noqa: F401
import threading
def getCorrId(data):
    corrId = data[8:12]
    print(corrId, int.from_bytes(corrId, byteorder="big"))
    return corrId
def checkApiVersion(data):
    errCode = 35
    apiVersion = int.from_bytes(data[6:8], byteorder="big")
    if 0 <= apiVersion <= 4:
        errCode = 0
    return errCode.to_bytes(2, byteorder="big")

def handle_client(addr,port):
    try:
        while True:
            a= 30
            data = addr.recv(1024) ## each time loop iterates server waits for incoming data then only proceed 
            ##we used while True to allow continues processing of multiple requests without restarting the server
            if not data: # Client disconnected
                print(f"Client {port} disconnected")
                break

            corrId = getCorrId(data)
            errorCode = checkApiVersion(data)
            apiKey = data[4:6]
            api_keys_bytes = b"".join([
                int(18).to_bytes(2, byteorder="big"), # APIVersions key
                int(0).to_bytes(2, byteorder="big"), # Min version
                int(4).to_bytes(2, byteorder="big"), # Max version
                int(0).to_bytes(1, byteorder="big"), # Extra byte
                int(75).to_bytes(2, byteorder="big"), # DescribeTopicPartitions key
                int(0).to_bytes(2, byteorder="big"), # Min version
                int(0).to_bytes(2, byteorder="big"), # Max version
                int(0).to_bytes(1, byteorder="big") # Extra byte
            ])

            
            #Combine the array of bytes into a single byte sequence 
            #api_keys_bytes = b"".join(api_keys_array)
            a=19
            addr.sendall(
                a.to_bytes(4, byteorder="big")
                + corrId
                + errorCode
                + int(2).to_bytes(1, byteorder="big") #number of api keys
                + api_keys_bytes       
                + int(0).to_bytes(4, byteorder="big")#throttle time  
            )
            # response_data = (
            # corrId
            # + errorCode
            # + int(1).to_bytes(1, byteorder="big") # number of API keys
            # + api_keys_bytes
            # + int(0).to_bytes(4, byteorder="big") # throttle time
            # )
            # a = len(response_data)
            # addr.sendall(
            # a.to_bytes(4, byteorder="big") # Response size prefix
            # + response_data
            # )
    except Exception as e:
        print(f"Error handling client: {e}")
    finally:
        print(a)
        addr.close()
        
def main():
    # You can use print statements as follows for debugging,
    # they'll be visible when running tests.
    print("Logs from your program will appear here!")
    # Uncomment this to pass the first stage
    #
    server = socket.create_server(("localhost", 9092), reuse_port=True)
    
    while True:
       addr, port = server.accept() # wait for client   
       client_thread=threading.Thread(target=handle_client,args=(addr,port))
       client_thread.start()

if __name__ == " __main__":
    main()

```

when everything got its only accepting the first api key not accepting the 2nd one

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/1/137d13cdb205debab3dba3040307dd2f4eaa85bc.png)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [April 11, 2025, 3:53pm UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/9 "2025-04-11T15:53:14Z")

</div>

@faith-in-het, the **ApiVersions** array is a [COMPACT\_ARRAY](https://kafka.apache.org/protocol.html#protocol_types), which means its length should be encoded as the actual number of entries **plus one**.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/3/3a16885494791c1fe208a8241e5c2dba19c556fd.png)

In this [BinSpec example](https://binspec.org/kafka-api-versions-Response-v4?highlight=10-10), there are 3 ApiVersion entries, so the length is encoded as 4:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/f/ff48eceec2ae65edef0c89ff8dbc819300b620c9.png)

Let me know if you need further clarification!

---

<div class="post-metadata">

**Author:** ![faith-in-het](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/faith-in-het/32/8759_2.png) [@faith-in-het](https://forum.codecrafters.io/u/faith-in-het)\
**Post date:** [April 11, 2025, 8:23pm UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/10 "2025-04-11T20:23:54Z")

</div>

So The issue was I have to write length in unsigned\_varient that i got by this code

```auto
def encode_unsigned_varint(value):
    """Encode an integer as UNSIGNED_VARINT."""
    encoded = []
    while value > 0x7F:
        encoded.append((value & 0x7F) | 0x80)
        value >>= 7
    encoded.append(value)
    return bytes(encoded)

```

Thank you soo much for your help bro bless you 🙇‍♂️

---

<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:** [April 16, 2025, 8:24pm UTC](https://forum.codecrafters.io/t/describetopicpartion-error/8503/11 "2025-04-16T20:24:39Z")

</div>

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