diff --git a/kgb/spies.py b/kgb/spies.py
index 8ca1d27f2efec536a9151abc4802f0db2b6b9787..06bbc2a5f3334064387672bb1c81bee66052c419 100644
--- a/kgb/spies.py
+++ b/kgb/spies.py
@@ -1006,6 +1006,33 @@
             if pyver >= (3, 11):
                 replace_kwargs['co_qualname'] = old_code.co_qualname
 
+            # Python 3.13+ includes a warning when assigning a code object if
+            # the generator flag doesn't match. The actual bug that caused them
+            # to add this warning can be found here:
+            #
+            #     https://github.com/python/cpython/issues/81137
+            #
+            # This is a pretty isolated, unique case where the user was
+            # replacing the code object for a lambda with the gi_code from a
+            # generator expression, causing CPython to crash. This is purely
+            # an issue with the gi_code object, and does not affect generator
+            # functions (or even a lambda that returns a generator expression)
+            # in the same way, so kgb's use is fine. We therefore mirror these
+            # flags onto our replacement code object to prevent the warning.
+            #
+            # Our forwarding function won't have any of these flags set, so we
+            # can just copy the positive case for all of them if they exist on
+            # the function we're replacing.
+            #
+            # We only do this on 3.11+ because on earlier versions this causes
+            # the spy to not execute.
+            if pyver >= (3, 11):
+                mirror_flags = old_code.co_flags & (
+                    inspect.CO_GENERATOR |
+                    inspect.CO_COROUTINE |
+                    inspect.CO_ASYNC_GENERATOR)
+                replace_kwargs['co_flags'] = temp_code.co_flags | mirror_flags
+
             new_code = temp_code.replace(**replace_kwargs)
         else:
             # Python <= 3.7
diff --git a/kgb/tests/test_function_spy.py b/kgb/tests/test_function_spy.py
index ab223e7bd8a47035ddca709c50f0d5cf68a365be..eff1706a65cbd88784e64b65dc5ff0eae4c569f5 100644
--- a/kgb/tests/test_function_spy.py
+++ b/kgb/tests/test_function_spy.py
@@ -7,8 +7,8 @@
 import traceback
 import types
 import unittest
+import warnings
 from contextlib import contextmanager
-from warnings import catch_warnings
 
 from kgb.errors import ExistingSpyError, IncompatibleFunctionError
 from kgb.pycompat import text_type
@@ -23,7 +23,7 @@
 def require_getargspec(func):
     """Require getargspec for a unit test.
 
-    If not available, the test will be skippd.
+    If not available, the test will be skipped.
 
     Args:
         func (callable):
@@ -32,7 +32,7 @@
     @functools.wraps(func)
     def _wrap(*args, **kwargs):
         if has_getargspec:
-            with catch_warnings(record=True):
+            with warnings.catch_warnings(record=True):
                 return func(*args, **kwargs)
         else:
             raise unittest.SkipTest(
@@ -1885,3 +1885,34 @@
         self.assertEqual(varargs, 'args')
         self.assertEqual(keywords, 'kwargs')
         self.assertEqual(defaults, (2, 5))
+
+    def test_spy_on_generator(self) -> None:
+        """Testing FunctionSpy with a generator function"""
+        # Python 3.13+ added a deprecation warning when replacing a generator
+        # function. This test verifies that our fix for this does not trigger a
+        # DeprecationWarning. See the comments in FunctionSpy._build_spy_code
+        # for details on the issue.
+        def _generator_function():
+            yield 'a'
+            yield 'b'
+            yield 'c'
+
+        def _fake_generator():
+            yield 'x'
+            yield 'y'
+            yield 'z'
+
+        with warnings.catch_warnings():
+            warnings.simplefilter('error', DeprecationWarning)
+
+            spy = self.agency.spy_on(_generator_function,
+                                     call_fake=_fake_generator)
+            results = list(_generator_function())
+            self.assertEqual(results, ['x', 'y', 'z'])
+            spy.unspy()
+
+            spy = self.agency.spy_on(_generator_function)
+
+            results = list(_generator_function())
+            self.assertEqual(results, ['a', 'b', 'c'])
+            spy.unspy()
