{"record":{"id":"55c8d278e8072062","repo":"QuantConnect/Lean","slug":"expected-3-futures-chains-from-history-request-bu","errorCode":null,"errorMessage":"Expected 3 futures chains from history request, but got {historical_futures_data_df.index.levshape[1]}","messagePattern":"Expected 3 futures chains from history request, but got (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/FutureUniverseHistoryRegressionAlgorithm.py","lineNumber":32,"sourceCode":"from AlgorithmImports import *\n\n### <summary>\n### Regression algorithm testing history requests for <see cref=\"FutureUniverse\"/> type work as expected\n### and return the same data as the futures chain provider.\n### </summary>\nclass OptionUniverseHistoryRegressionAlgorithm(QCAlgorithm):\n\n    def initialize(self):\n        self.set_start_date(2013, 10, 11)\n        self.set_end_date(2013, 10, 11)\n\n        future = self.add_future(Futures.Indices.SP_500_E_MINI).symbol\n\n        historical_futures_data_df = self.history(FutureUniverse, future, 3, flatten=True)\n\n        # Level 0 of the multi-index is the date, we expect 3 dates, 3 future chains\n        if historical_futures_data_df.index.levshape[0] != 3:\n            raise RegressionTestException(f\"Expected 3 futures chains from history request, \"\n                                          f\"but got {historical_futures_data_df.index.levshape[1]}\")\n\n        for date in historical_futures_data_df.index.levels[0]:\n            expected_chain = list(self.future_chain_provider.get_future_contract_list(future, date))\n            expected_chain_count = len(expected_chain)\n\n            actual_chain = historical_futures_data_df.loc[date]\n            actual_chain_count = len(actual_chain)\n\n            if expected_chain_count != actual_chain_count:\n                raise RegressionTestException(f\"Expected {expected_chain_count} futures in chain on {date}, \"\n                                              f\"but got {actual_chain_count}\")\n\n            for i, symbol in enumerate(actual_chain.index):\n                expected_symbol = expected_chain[i]\n                if symbol != expected_symbol:\n                    raise RegressionTestException(f\"Expected symbol {expected_symbol} at index \"\n                                                  f\" {i} on {date}, but got {symbol}\")","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/FutureUniverseHistoryRegressionAlgorithm.py#L14-L50","documentation":"Regression check that a history(FutureUniverse, symbol, 3, flatten=True) call returns exactly 3 dated future chains. It inspects the pandas multi-index shape (levshape[0]) and asserts it equals 3. NOTE: the message prints levshape[1] (the contract level) while the comparison uses levshape[0] (the date level), so the diagnostic text is misleading.","triggerScenarios":"Asking for 3 bars but the data only spans fewer distinct trading days (start/end date too narrow); FutureUniverse history not materialising a multi-index when fewer chains are returned; flatten=True changing the index arity unexpectedly.","commonSituations":"set_start_date == set_end_date so only one chain is available; data provider returning empty/short history for the requested symbol; engine changes to FutureUniverse history serialization altering the index layout.","solutions":["Widen the date window or raise the history bar count so 3 distinct future chains exist.","Print historical_futures_data_df.index.levshape to confirm both levels before trusting the message text (it shows the wrong level).","Verify the FutureUniverse type is supported for history in the current engine version.","Ensure flatten=True is appropriate — without it the index arity differs."],"exampleFix":"// before\nif historical_futures_data_df.index.levshape[0] != 3:\n    raise RegressionTestException(f\"...but got {historical_futures_data_df.index.levshape[1]}\")\n// after - print the correct level\nif historical_futures_data_df.index.levshape[0] != 3:\n    raise RegressionTestException(f\"...but got {historical_futures_data_df.index.levshape[0]}\")","handlingStrategy":"validation","validationCode":"# Validate the multi-index shape before asserting\ndf = self.history(FutureUniverse, future, 3, flatten=True)\nif len(df.index.levels[0]) != 3:\n    self.debug(f\"History returned {len(df.index.levels[0])} chains; levshape={df.index.levshape}\")","typeGuard":"def history_has_n_chains(df, n: int) -> bool:\n    return len(df.index.levels) > 0 and len(df.index.levels[0]) == n","tryCatchPattern":null,"preventionTips":["Print df.index.levshape before trusting the message text (it references the wrong level).","Ensure the date window / bar count yields the expected number of chains.","Keep flatten=True consistent with the index-arity assumptions.","Re-verify after engine upgrades that FutureUniverse history layout is unchanged."],"tags":["quantconnect","futures","future-universe","history-request","pandas","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}