{"record":{"id":"11e343671d6671e7","repo":"microsoft/graphrag","slug":"k-must-be-an-integer-0","errorCode":null,"errorMessage":"k must be an integer > 0","messagePattern":"k must be an integer > 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/graphrag/graphrag/prompt_tune/loader/input.py","lineNumber":83,"sourceCode":"        doc_dict = dataclasses.asdict(doc)\n        chunks = chunk_document(doc_dict, chunker)\n        all_chunks.extend(chunks)\n\n    chunks_df = pd.DataFrame({\"text\": all_chunks})\n\n    # Depending on the select method, build the dataset\n    if limit <= 0 or limit > len(chunks_df):\n        logger.warning(f\"Limit out of range, using default number of chunks: {LIMIT}\")  # noqa: G004\n        limit = LIMIT\n\n    if select_method == DocSelectionType.TOP:\n        chunks_df = chunks_df[:limit]\n    elif select_method == DocSelectionType.RANDOM:\n        chunks_df = chunks_df.sample(n=limit)\n    elif select_method == DocSelectionType.AUTO:\n        if k is None or k <= 0:\n            msg = \"k must be an integer > 0\"\n            raise ValueError(msg)\n\n        \"\"\"Convert text chunks into dense text embeddings.\"\"\"\n        sampled_text_chunks = chunks_df.sample(n=min(n_subset_max, len(chunks_df)))[\n            \"text\"\n        ].tolist()\n\n        embedding_results = await run_embed_text(\n            sampled_text_chunks,\n            callbacks=NoopWorkflowCallbacks(),\n            model=model,\n            tokenizer=tokenizer,\n            batch_size=config.embed_text.batch_size,\n            batch_max_tokens=config.embed_text.batch_max_tokens,\n            num_threads=config.concurrent_requests,\n        )\n        embeddings = np.array(embedding_results.embeddings)\n        chunks_df = _sample_chunks_from_embeddings(chunks_df, embeddings, k=k)\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/microsoft/graphrag/blob/f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704/packages/graphrag/graphrag/prompt_tune/loader/input.py#L65-L101","documentation":"When using DocSelectionType.AUTO chunk selection in GraphRAG prompt tuning (load_docs_in_chunks), a subsampling size k must be supplied and be a positive integer. AUTO mode samples min(k, len(chunks)) documents to embed for clustering-based selection, so k=None or k<=0 makes sampling impossible and raises this ValueError.","triggerScenarios":"Calling load_docs_in_chunks(..., select_method=DocSelectionType.AUTO) with chunk_size/k omitted (None) or set to 0 or a negative number; programmatic calls to generate_indexing_prompts that forward a user-supplied k without validation.","commonSituations":"CLI prompt-tuning runs where --chunk-size wasn't passed with auto selection; passing k as a string like '0'; refactors where the k parameter default changed to None.","solutions":["Pass a positive integer for chunk_size/k, e.g. load_docs_in_chunks(..., select_method=DocSelectionType.AUTO, chunk_size=200)","If you want all/limited/random selection, use DocSelectionType.ALL, TOP, or RANDOM which don't require k","Validate k before calling: if using user input, coerce to int and check > 0"],"exampleFix":"# before\nload_docs_in_chunks(df, select_method=DocSelectionType.AUTO, k=None)\n# after\nload_docs_in_chunks(df, select_method=DocSelectionType.AUTO, k=200)","handlingStrategy":"validation","validationCode":"k = int(k) if k is not None else None\nif select_method == DocSelectionType.AUTO and (k is None or k <= 0):\n    k = 200  # sane default\nchunks = load_docs_in_chunks(df, select_method=select_method, chunk_size=k)","typeGuard":"def is_valid_k(k: object) -> bool:\n    return isinstance(k, int) and not isinstance(k, bool) and k > 0","tryCatchPattern":null,"preventionTips":["Validate user-supplied sampling sizes at CLI/config boundaries","Remember AUTO is the only mode requiring a positive k"],"tags":["graphrag","prompt-tuning","validation","argument-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704","analyzedAt":"2026-08-27T11:16:29.677Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}