You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
testnet API returns invalid token indices causing IndexError #275
The testnet API (api.hyperliquid-testnet.xyz) returns spotMeta data with invalid token indices that cause an IndexError when initializing the Info class.
Error
IndexError: list index out of range
At hyperliquid/info.py:48:
base_info=spot_meta["tokens"][base] # IndexError when base >= len(tokens)
Root Cause
The testnet spotMeta endpoint returns 1575 tokens but 19 universe entries have token indices >= 1575 (e.g., base: 1576, 1577, 1583).
Here's our local workaround - we forked the hyperliquid/info.py module and added defensive error handling:
PATCH: Handle testnet API gracefully - spot_meta may be malformed
if spot_meta is None:
try:
spot_meta = self.spot_meta()
except Exception as e:
# Testnet API sometimes returns malformed data - skip spot initialization
spot_meta = None
if spot_meta is not None:
try:
for spot_info in spot_meta.get("universe", []):
asset = spot_info["index"] + 10000
self.coin_to_asset[spot_info["name"]] = asset
self.name_to_coin[spot_info["name"]] = spot_info["name"]
base, quote = spot_info["tokens"]
# PATCH: Safe access to tokens array
if "tokens" in spot_meta and base < len(spot_meta["tokens"]) and quote < len(spot_meta["tokens"]):
base_info = spot_meta["tokens"][base]
quote_info = spot_meta["tokens"][quote]
self.asset_to_sz_decimals[asset] = base_info["szDecimals"]
name = f'{base_info["name"]}/{quote_info["name"]}'
if name not in self.name_to_coin:
self.name_to_coin[name] = spot_info["name"]
except (IndexError, KeyError, TypeError) as e:
# Testnet API bug - skip spot initialization, perp trading still works
pass
Key differences from the PR #277 fix:
We wrap the entire spot_meta() fetch in a try-except to handle malformed responses
We check bounds before accessing spot_meta["tokens"][base] and [quote]
We catch IndexError, KeyError, TypeError and gracefully skip spot initialization
Perp trading continues to work even when spot data is broken
This has been running in production on our testnet deployment for several days without issues.
@KarolakTibia I see how the workaround differs from the PR. It produces a smaller name_to_coin since certain base/quote pairs fall out of range, whereas we should be leveraging the index from the info field. Of course, this might be negligible for testnet purposes.
If you have a different perspective on how base_info and quote_info indices/index should be handled, I’d love to hear your thoughts.
Activity
pat-mok commentedon Mar 8, 2026
When will the SDK be updated to fix this issue? I am launching the bot test in vps. So a local fix is not suitable for me.
XYTYX commentedon Mar 9, 2026
Getting the same error. In reality the issue is whoever messed it up not importing the correct tokens to testnet.
bug-or-feature commentedon Mar 9, 2026
@KarolakTibia thanks for flagging this. What is your local workaround?
Kaldora-party commentedon Mar 9, 2026
Here's our local workaround - we forked the
hyperliquid/info.pymodule and added defensive error handling:Key differences from the PR #277 fix:
spot_meta()fetch in a try-except to handle malformed responsesspot_meta["tokens"][base]and[quote]IndexError, KeyError, TypeErrorand gracefully skip spot initializationThis has been running in production on our testnet deployment for several days without issues.
bug-or-feature commentedon Mar 9, 2026
thank you
vagetablechicken commentedon Mar 11, 2026
@KarolakTibia I see how the workaround differs from the PR. It produces a smaller name_to_coin since certain base/quote pairs fall out of range, whereas we should be leveraging the index from the info field. Of course, this might be negligible for testnet purposes.
If you have a different perspective on how base_info and quote_info indices/index should be handled, I’d love to hear your thoughts.
Kaldora-party commentedon Mar 11, 2026
@vagetablechicken
Fix Implemented
I've implemented the suggested fix from the latest comment and it's working well. Here's the approach:
The Problem
The testnet API returns malformed
spot_metawhere token indices inuniverseentries exceed the bounds of thetokensarray, causingIndexError.The Solution
Instead of array position lookup, build an index-based token dictionary:
Why This Works
indexfield that's stable regardless of array positiontokensarray in universe entries contains these index values, not array positionsindexfield, we avoid the array bounds issue entirelyTesting
Verified with multiple test scenarios including:
Thanks for the suggestion to "leverage the index from the info field" - that was the key insight!
fix: use index-based token lookup to handle malformed spot_meta (issue …
mifth commentedon Mar 29, 2026
It seems I made a duplicate:
#283