“AssertionError: san() and lan() expect move to be legal or null” In Python Chess Program

import chess
import chess.pgn
import chess.engine
import tkinter as tk
from tkinter import simpledialog
import os
from tqdm import tqdm

# Function to get FEN position from a popup window
def get_fen_position():
    root = tk.Tk()
    root.withdraw()
    fen_position = simpledialog.askstring("Input", "Paste the FEN position:")
    root.destroy()
    return fen_position

# Function to evaluate position using Stockfish
def evaluate_position(board, engine, depth_white=17, depth_black=6, multipv=3):
    if board.turn == chess.WHITE:
        result = engine.analyse(board, chess.engine.Limit(depth=depth_white))
        move = result['pv'][0]
        board.push(move)
    else:
        infos = engine.analyse(board, chess.engine.Limit(depth=depth_black), multipv=multipv)
        moves = [info['pv'][0] for info in infos if 'pv' in info]
        for move in moves:
            board.push(move)
            response = engine.analyse(board, chess.engine.Limit(depth=depth_white))
            board.pop()
    return board

# Function to recursively evaluate and write moves to PGN
def recursive_evaluation(board, engine, node, depth=1, max_depth=8, pbar=None):
    if depth > max_depth:
        return

    board = evaluate_position(board, engine)
    
    if board.turn == chess.BLACK:
        infos = engine.analyse(board, chess.engine.Limit(depth=6), multipv=3)
        for info in infos:
            if 'pv' not in info:
                continue
            new_board = board.copy()
            move = info['pv'][0]
            new_board.push(move)
            new_node = node.add_variation(move)
            recursive_evaluation(new_board, engine, new_node, depth + 1, max_depth, pbar)
    else:
        move = board.peek()
        new_node = node.add_variation(move)
        recursive_evaluation(board, engine, new_node, depth + 1, max_depth, pbar)
    
    if pbar:
        pbar.update(1)

# Main function to run the script
def main():
    # Path to Stockfish executable
    stockfish_path = 'C:/Users/Admintrator/Downloads/stockfish-windows-x86-64-avx2/stockfish/stockfish-windows-x86-64-avx2.exe'

    # Path to the existing PGN file
    pgn_path = "C:/Users/Admintrator/OneDrive - Nguyen Sieu School/Documents/EnCroissant/Nhị Cuộc Phi Đao.pgn"

    # Initialize engine
    engine = chess.engine.SimpleEngine.popen_uci(stockfish_path)

    # Set Stockfish options for threads and hash size
    engine.configure({"Threads": 16, "Hash": 8192})

    # Get FEN position from user
    fen = get_fen_position()

    # Load the existing PGN file
    with open(pgn_path, "r", encoding="utf-8") as pgn:
        game = chess.pgn.read_game(pgn)

    # Set up the board with the given FEN position
    board = chess.Board(fen)
    node = game.end()

    # Initialize progress bar
    max_depth = 8
    total_steps = sum(3**i for i in range(max_depth))  # Total steps for progress bar
    with tqdm(total=total_steps, desc="Evaluating Position") as pbar:
        # Evaluate position and add moves to PGN
        recursive_evaluation(board, engine, node, max_depth=max_depth, pbar=pbar)

    # Write the updated game to the PGN file
    with open("evaluated_game.pgn", "w", encoding="utf-8") as pgn_out:
        exporter = chess.pgn.FileExporter(pgn_out)
        game.accept(exporter)
    
    # Close engine
    engine.quit()

if __name__ == "__main__":
    main()

I wanted to make a tool that uses stockfish to automatically continue to evaluate a certain position in a .PGN file. Somehow it runs 3/4 of the way and say this:

Traceback (most recent call last):
  File "C:UsersAdmintratorDownloadsFEN continue.py", line 98, in <module>
    main()
  File "C:UsersAdmintratorDownloadsFEN continue.py", line 92, in main
    game.accept(exporter)
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschesspgn.py", line 883, in accept
    self.variations[0]._accept(board, visitor)
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschesspgn.py", line 756, in _accept
    top.node._accept_node(parent_board, visitor)
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschesspgn.py", line 733, in _accept_node
    visitor.visit_move(parent_board, self.move)
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschesspgn.py", line 1403, in visit_move
    self.write_token(board.san(move) + " ")
                     ^^^^^^^^^^^^^^^
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschess__init__.py", line 2866, in san
    return self._algebraic(move)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschess__init__.py", line 2879, in _algebraic
    san = self._algebraic_and_push(move, long=long)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschess__init__.py", line 2884, in _algebraic_and_push
    san = self._algebraic_without_suffix(move, long=long)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersAdmintratorAppDataLocalProgramsPythonPython311Libsite-packageschess__init__.py", line 2920, in _algebraic_without_suffix
    assert piece_type, f"san() and lan() expect move to be legal or null, but got {move} in {self.fen()}"
AssertionError: san() and lan() expect move to be legal or null, but got f8b8 in r6K/p1pbqppp/2nP3n/bP6/2B1P3/1Qp2N2/P4PPP/RNB2RK1 b - - 0 11

I have no idea what to fix here, tried using ChatGPT but still can’t get it to work. Gone through every single same error on Stackoverflow but still can’t fix it.

New contributor

Vương Quốc Hiển is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật