{"record":{"id":"79267287e5a17501","repo":"langchain-ai/langchain","slug":"expected-keys-sorted-expected-keys-do-not-match","errorCode":null,"errorMessage":"Expected keys {sorted(expected_keys)} do not match parameter names {sorted(parameter_names)} of get_session_history.","messagePattern":"Expected keys (.+?) do not match parameter names (.+?) of get_session_history\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/history.py","lineNumber":619,"sourceCode":"\n        if len(expected_keys) == 1:\n            if parameter_names:\n                # If arity = 1, then invoke function by positional arguments\n                message_history = self.get_session_history(\n                    configurable[expected_keys[0]]\n                )\n            else:\n                if not config:\n                    config[\"configurable\"] = {}\n                message_history = self.get_session_history()\n        else:\n            # otherwise verify that names of keys patch and invoke by named arguments\n            if set(expected_keys) != set(parameter_names):\n                msg = (\n                    f\"Expected keys {sorted(expected_keys)} do not match parameter \"\n                    f\"names {sorted(parameter_names)} of get_session_history.\"\n                )\n                raise ValueError(msg)\n\n            message_history = self.get_session_history(\n                **{key: configurable[key] for key in expected_keys}\n            )\n        config[\"configurable\"][\"message_history\"] = message_history\n        return config\n\n\ndef _get_parameter_names(callable_: GetSessionHistoryCallable) -> list[str]:\n    \"\"\"Get the parameter names of the `Callable`.\"\"\"\n    sig = inspect.signature(callable_)\n    return list(sig.parameters.keys())\n","sourceCodeStart":601,"sourceCodeEnd":632,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/history.py#L601-L632","documentation":"When more than one key is expected (multi-parameter get_session_history or multiple history_factory_config fields), RunnableWithMessageHistory invokes the callable by keyword arguments and requires that the set of expected keys exactly equals the set of parameter names in get_session_history's signature. Any mismatch raises this ValueError listing both sets.","triggerScenarios":"A get_session_history(session_id, user_id) factory but history_factory_config declaring fields named differently (e.g. session_id, tenant_id); declaring two ConfigurableFieldSpec ids that do not match the function parameters.","commonSituations":"Multi-tenant history stores keyed by (user_id, session_id); refactoring parameter names without updating ConfigurableFieldSpec ids; copying a factory from docs with different naming.","solutions":["Align the ConfigurableFieldSpec ids with the get_session_history parameter names exactly (or vice versa)","Simplify: if possible use a single-parameter factory taking just session_id","Verify with inspect.signature(get_session_history) and compare against the field ids you pass to history_factory_config"],"exampleFix":"# before\ndef get_history(session_id: str, tenant: str): ...\nhistory_factory_config=[\n    ConfigurableFieldSpec(id=\"session_id\", ...),\n    ConfigurableFieldSpec(id=\"tenant_id\", ...),  # mismatch: param is `tenant`\n]\n# after\nConfigurableFieldSpec(id=\"tenant\", ...)  # matches parameter name","handlingStrategy":"validation","validationCode":"import inspect\nparams = set(inspect.signature(get_session_history).parameters)\nexpected = {f.id for f in history_factory_config}\nassert params == expected, f\"{params} != {expected}\"","typeGuard":null,"tryCatchPattern":"try:\n    wrapped.invoke(x, cfg)\nexcept ValueError as e:\n    if \"do not match parameter\" in str(e):\n        fix_field_ids_to_match_signature()","preventionTips":["Make ConfigurableFieldSpec ids literally equal the factory's parameter names","Simplest: single-parameter get_session_history(session_id)"],"tags":["runnable","message-history","config","factory-signature","valueerror"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}